IDbCommonInterceptor永远不会被调用

本文关键字:调用 永远 IDbCommonInterceptor | 更新日期: 2023-09-27 18:26:46

我已经创建了一个基于实体框架的应用程序。为了分离数据库的读取和写入,我尝试使用IDbCommonInterceptor。至于我的想法,IDbCommonInterceptor将捕获EF对数据库的访问,我可以将连接更改为另一个连接。连接正常,我可以正常从数据库中获得结果。然而,唯一令人尴尬的是,我的自定义DbCommandInterceptor类甚至还没有被调用

我的自定义拦截器:

public class EFCommandInterceptor: IDbCommandInterceptor
{
    public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }
    public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync,  command.CommandText));
    }
    public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
    {
        LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }
    public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
    {
        LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }
    public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }
    public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }
    private void LogInfo(string command, string commandText)
    {
        //I add a break point here, but it has never been called;
    }
}

调用拦截器的代码:

var cmdInterceptor = new EFCommandInterceptor();
        System.Data.Entity.Infrastructure.Interception.DbInterception.Add(cmdInterceptor);
        using (var context = new CSMDBContainer())
        {
            var task = context.T_TASK.FirstOrDefault();
            context.SaveChanges();
        }
        System.Data.Entity.Infrastructure.Interception.DbInterception.Remove(cmdInterceptor);

配置文件中的实体框架部分:

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

任何建议都将不胜感激!

IDbCommonInterceptor永远不会被调用

最后,我解决了这个问题。路径是,EF生成的Context类继承了ObjectContext

在我重新构建edmx文件并使Context类继承DbContext之后,问题就解决了。