Page 1 of 1

Virtual Private Database

Posted: Tue 15 Jul 2014 21:05
by suhman
Hi,

Do you have any samples on using Virtual Private Database in Entity framework Code First approach?

http://docs.oracle.com/cd/E16655_01/net ... m#DBSEG007

Re: Virtual Private Database

Posted: Fri 18 Jul 2014 11:23
by MariiaI
There is no such sample at the moment. We will investigate the question and notify you about the results as soon as possible.
Have you encountered any particular difficulties when employing Virtual Private Database with dotConnect for Oracle?

Re: Virtual Private Database

Posted: Fri 18 Jul 2014 12:50
by suhman
As of now, we have a store proc that apply the VPD Key of the client in the connection context. While reviewing your component, I tried executing the existing store proc before executing the any linq queries and it seems working fine. Oracle automatically applies the VPD key on the queries executed by Entity Framework. I am looking for a better way to apply the VPD key in the database context instead of executing the store proc.

Re: Virtual Private Database

Posted: Mon 21 Jul 2014 14:22
by Shalex
There are two ways:
1) you can specify a command that should be executed immediately after establishing the connection via the Initialization Command connection string parameter
2) handle the OracleConnection.StateChange event and call your stored procedure each time when connection becomes ConnectionState.Open.

Re: Virtual Private Database

Posted: Mon 21 Jul 2014 16:18
by suhman
I can't use the 1st way because I get VPD Key only after connecting the database. I tried the #2 way as below.

Code: Select all

 void Connection_StateChange(object sender, System.Data.StateChangeEventArgs e)
        {
            if (e.CurrentState == System.Data.ConnectionState.Open && e.OriginalState == System.Data.ConnectionState.Closed)
                ObjectContext.Context.Database.ExecuteSqlCommand("BEGIN sp_vpd('ABCD1234'); END;");
        }
I get an error message
The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.

Re: Virtual Private Database

Posted: Thu 24 Jul 2014 08:37
by Shalex
Try this code:

Code: Select all

class Program
{
    static void Main(string[] args)
    {
        OracleMonitor monitor = new OracleMonitor() { IsActive = true };
        using(var context = new MyDbContext.MyEntities()){
            context.Database.Connection.StateChange += conn_StateChange;
            var depts = context.DEPTs.ToList();
            // ...
        }
    }

    static void conn_StateChange(object sender, System.Data.StateChangeEventArgs e)
    {
        if (e.CurrentState == System.Data.ConnectionState.Open && e.OriginalState == System.Data.ConnectionState.Closed) { 
            var cmd = (sender as OracleConnection).CreateCommand();
            cmd.CommandText = "BEGIN sp_vpd('ABCD1234'); END;";
            cmd.ExecuteNonQuery();
        }
    }
}