Home > Software engineering >  Use Oracle's DBMS_SESSION.set_context in Entity Framework Core
Use Oracle's DBMS_SESSION.set_context in Entity Framework Core

Time:02-11

I have the need to show on my .net core program an Oracle view that in the database has some columns filtered by

... WHERE dictionary.LANGUAGE = SYS_CONTEXT ('CLIENTCONTEXT', 'LANGUAGE');

Obviously fetching the data directly on the application makes those columns return a null value.

I would need to implement something like this

OracleCommand cmd = new OracleCommand(String.Format("BEGIN DBMS_SESSION.SET_CONTEXT('CLIENTCONTEXT', 'LANGUAGE', '{0}'); END;", ActualLanguage), ORACLEconn as OracleConnection);
cmd.ExecuteNonQuery();

How would I go about implementing the code using Entity Framework Core? Would I need to call it once on the model creation or every time I create a new DbContext?

Thank you.

CodePudding user response:

If you have a DbContext constructor that's only used when you want the session context set, you can force the connection open, and it will remain open until your DbContext is disposed. eg

public Db(DbContextOptions opts) : base(opts)
{
    this.Database.OpenConnection();
    this.Database.ExecuteSqlRaw("BEGIN DBMS_SESSION.SET_CONTEXT('CLIENTCONTEXT', 'LANGUAGE', 'whatever'); END;");
}

Or you can use an Interceptor to run the command every time a connection is opened.

  • Related