Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

EntityFramework Oracle.ManagedDataAccess.Client.OracleException: 'ORA-01918: user 'dbo' does not exist'

While trying to use code first approach to connect to Oracle database using ASP NET C#.
I received the following exception
Oracle.ManagedDataAccess.Client.OracleException: 'ORA-01918: user 'dbo' does not exist'
My current User Is IT_USER
The Reason for the error is that Oracle tried to create my tables which are in my first migration into dbo schema as SQL Server do, but in Oracle it is a User.

So you need to till Entity Framework where is my Table Should be
You have two solutions

First is to use DbModelBuilder.HasDefaultSchema
This will make all of your tables in this schema.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.HasDefaultSchema("IT_USER"); 
    base.OnModelCreating(modelBuilder);
}

Second is to use System.ComponentModel.DataAnnotations.Schema.Table
This will give you the ability to change the schema when ever you want
[Table("STD_FA_VALIDATION", Schema ="IT_USER")] 
public class STD_FA_VALIDATION
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int SGBSTDN_PIDM { get; set; }
    public string NAT_CHANGE_IND { get; set; }
    public string WORK_IND { get; set; }
    public DateTime NAT_PROC_DATE { get; set; }
    public DateTime WORK_PROC_DATE { get; set; }
 }

How to create OWA_COOKIE PLSQL

The Oracle Web Agent (OWA) provide the web users with an easy way to send and get cookies throw the browser header using OWA_COOKIE package.
there is two types can be used for OWA_COOKIE
  1. type vc_arr is table of varchar2(4000) index by binary_integer.
  2. type cookie is record (name varchar2(4000),vals vc_arr,num_vals integer); 
  1. Open Header
  2. Send Cookie 
  3. Close Header 
  4. Redirect to another page. 
don't forget to close the header. 

if for some reason you find the cookie value printed into the screen then your document header maybe was opened  and closed before your cookie code. 

BEGIN
OWA_UTIL.MIME_HEADER ('text/html', FALSE);
OWA_COOKIE.SEND (NAME    => 'REGIND',VALUE   => TWBKBSSF.F_ENCODE ('N'));
OWA_UTIL.HTTP_HEADER_CLOSE;
OWA_UTIL.REDIRECT_URL ('xwskstrg.P_RegsAddCrse');
END;
make sure that you encrypt the value of the cookie if it will contain a sensitive data.

Modify AWR Automatic Snapshot Settings

DBA_HIST_WR_CONTROL table shows the AWR snapshot settings.
In this example the retention period is specified as 1 month (43200 minutes) and the interval between each snapshot is 30 minutes.

BEGIN
DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS (INTERVAL => 30,RETENTION => 43200);
END

SELECT * FROM DBA_HIST_WR_CONTROL

After executing  DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS  procedure you can get the AWR Report for each 30 min the hole year.

Unable to create a constant value of type ‘’. Only primitive types or enumeration types are supported in this context.

the problem appears because you are using contains method in non primitive type
here is an example
Audit audit = new Audit();
List<int> ints = userReports.Select(y => y.ReportId).ToList();
List<ReportUserVisit> userReportLastMonthVisit = audit.GetAudit(userId, AuditType.Report)
                .Where(x => ints.Contains((int)x.ObjectId))
                .Select(x=> x.ObjectId)
                .GroupBy(x=>x.Value)
                .Select(x=>new ReportUserVisit { ReportId = x.Key, LastMonthVisitCount= x.Count() })
                .ToList();

note ints object, this will solve the problem.
because ints collection is list of int it can use contains method inside entity framework


Reset Identity column in SQL Server

If you need for some reason to reset the identity column of the sql server
this will do it

DBCC CHECKIDENT('tableName', RESEED, 0)

zero here means the count will begin from 1 

Change Data Capture (CDC) in SQL Server 2008

SQL Server 2005 introduced the new features of ‘after update’, ‘after insert’ and ‘after delete’ triggers that almost solved the problem of tracking changes in data. A better solution was introduced in SQL Server 2008 and is called Change Data Capture (CDC). CDC has allowed SQL Server developers to deliver SQL Server data archiving and capturing without any additional programming.


Enabling Change Data Capture on a Database:

first you need to know if the database enabled

using this SQL statement you can know,
USE masterGO
SELECT [name], database_id, is_cdc_enabled
FROM sys.databases
You can run the CDC at database level
USE NorthwindGO
EXEC sys.sp_cdc_enable_db
GO
clip_image001
some system tables will be created into the database
clip_image002
the cdc.captured_columns return list of captured columns
the cdc.change_tables reurn list of all tables enabled with capture
the cdc.ddl_history contains the history of the ddl changes scince the capture data enabled

Enable the CDC on one or more than one table
USE Northwind
Go
SELECT [Name], is_traced_by_cdc FROM sys.tables
Go
to enable the CDC on the tables you must make sure that the SQL Server Agent is running
when the CDC is enabled on tables, it will create two jobs and they will use the SQL Server Agent
clip_image003
If everything goes right, you will find inside the system tables a new table with this cdc.dbo_Categories_CT
inside this table you will find five columns important to you

  • __$start_lsn
  • __$end_lsn
  • __$seqval
  • __$operation
  • __$update_mask
There are two values are very important _$operation and _$update_mask
inside the _$operation there are some values

1. Delete Statement =1
2. Insert Statement = 2
3. Value before update = 3
4. Value after update = 4
The _$update_mask shows which columns where updated.
Now running the following
SELECT *
FROM Categories
GO
SELECT * from cdc.dbo_Categories_CT
Go
clip_image004
Insert into the table and run the above query again
clip_image006
Now you can find the Primary key of your original table into the new created table by CDC you can create a simple select to get all the rows from the CDC tables where they equal your primary key
SELECT * FROM cdc.dbo_Categories_CT
where cdc.dbo_Categories_CT.CategoryID = 9
And because this is insert statement then the _$operation = 2
Now to create an update statement
UPDATE [Northwind].[dbo].[Categories]
SET [CategoryName] = 'New Refat'
Description = 'something'
WHERE Categories.CategoryID = 9
UPDATE [Northwind].[dbo].[Categories]
SET [CategoryName] = 'New Refat',
Description = 'something else'
WHERE Categories.CategoryID = 9
clip_image007
I updated the record 2 times, the _$operation = 3 is the record before update and as you can see it will be the inserted record all the time,
and the _$operation = 4 is the record after update

Understanding Update mask:
the value of the _$update_mask is a hexadecimal stored into the field but it is a binary

For the insert and the delete the after converting the hexadecimal to a binary you will find the all the value is 0b1111 for exampleJ, this means all the columns modified.
In the update statement the binary value will contains 1’s and 0’s the from the right the 1’s means columns updated and the 0’s means there is no changes into these columns.
Also there is two columns _$start_lsn and _$end_lsn these two values are the log Sequential Number. This number is associated with committed transaction of the DML operation on the tracked table.
Disabling Change Data Capture on a table
to disable the CDC, you can do this over two levels on database level or the table level.
You need 3 things to disable the CDC, source_schema, source_name and capture_instance
It is easy to get the source_schema, source_name but it is hard to know the capture_instance
use this to get the capture_instance

clip_image008
You can disable as I said the CDC on the table level
USE Northwind;
GO
EXECUTE sys.sp_cdc_disable_table
@source_schema = N'Categories',
@source_name = N'dbo',
@capture_instance = N'dbo_Categories';
GO
And you will see the table created for the CDC will be droped
To disable the CDC on the Database level use this
USE Northwind;
GO
EXEC sys.sp_cdc_disable_db
GO
Capture Selected Column
CDC can be used for columns also
First I will enable the CDC on my Database again
USE Northwind;
GO
EXEC sys.sp_cdc_enable_db
GO
Now I will use the CDC on the columns level
USE Northwind;
GO
EXEC sys.sp_cdc_enable_table
@source_schema = N'dbo',
@source_name = N'Categories',
@role_name = NULL,
@captured_column_list = '[CategoryName],[Description]'
GO

filter attribute that checks whether current connection is secured

using APS.net Core to mark all website pages working with https protocol we will do this using IAuthorizationFilter. and here is an exampl...