Я использую реализацию IDbCommandInterceptor
:
public class MyInterceptor : IDbCommandInterceptor
{
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
var context = interceptionContext.DbContexts.FirstOrDefault();
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
}
Введено этим:
public class TestContext : System.Data.Entity.DbContext
{
// …
public TestContext()
: base("TestConnectionString")
{
Database.SetInitializer<TestContext>(null);
DbInterception.Add(new MyInterceptor());
}
}
(также пробовал в статическом конструкторе).
Но interceptionContext.DbContexts
всегда пусто. Как я могу получить экземпляр исполняемого контекста? Является ли это возможным?