永远不会调用IDbCommandInterceptor

时间:2015-12-15 09:49:06

标签: c# .net entity-framework entity-framework-6

我已经在Entity Framework上创建了一个应用程序。为了分离数据库的读写,我试图使用IDbCommandInterceptor。 至于我的想法,IDbCommandInterceptor将捕获从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>

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:1)

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

在我重建edmx文件并使Context类继承 DbContext 后,问题已经消失。