我正在使用 EF 6 并尝试使用 System.Data.Entity.Infrastructure.Interception 命名空间中的 IDbCommandInterceptor。
它适用于读取和更新,但是当我向数据库添加新实体时,NonQueryExecuted() 不会触发,NonQueryExecuting() 也不会触发。这是拦截器的正常行为还是我没有正确实施?
代码:
拦截器:
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
this.MonitorAndNotifySignalRService(command);
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
数据上下文库:
static Constructor()
{
Database.SetInitializer<TDataContext>(null); // turn off database initialization so the db schema is not changed from the model
DbInterception.Add(new Interceptor());
}