Reputation: 9039
The requirement is to execute a sql query using DbContext (EF 5 in DatabaseFirst mode) at the beginning of every Oracle session that DbContext might invoke.
Putting this in the constructor gives inconsistent results because there are instances where this sql query is not run at all when expected.
The setup is EF5 in DBFirst mode connecting to Oracle10gR2 using ODP.NET v12 Managed driver.
public partial class MyContext : DbContext
{
public MyContext(string connectionString)
: base(connectionString)
{
Database.ExecuteSqlCommand(Constants.SqlQuery);
}
}
I instantiate the context by passing the connecting string because the connection string needs to be dynamic as follows:
using(var context = new MyContext(GetConnectionString()))
{
...
...
context.SaveChanges();
}
Is there a way to ensure that this query is always run whenever an Oracle session is created?
Upvotes: 0
Views: 816
Reputation: 8386
Just implement the OnContextCreated()
method in your partial class:
partial void OnContextCreated()
{
this.ExecuteStoreCommand(Constants.SqlQuery, new object[] { });
}
Upvotes: 0
Reputation: 3024
You can extend the DbContext class and implement each method to do so. For example....
public KashDbContext : DbContext
{
public int SaveChanges()
{
Database.ExecuteSqlCommand(Constants.SqlQuery);
base.SaveChanges();
}
//Do for all methods
}
Upvotes: 1