IDbCommandInterceptor never be called

.net c# entity-framework entity-framework-6

Question

I have created an application base on Entity Framework. To separate reading and writing of the database, I am trying to use IDbCommandInterceptor. As for my thought, IDbCommandInterceptor will catch the access from EF to database and I could change the connection into another one. Connection is normal,I could get the result from database normally.However, The only embarassed thing is that, my custom DbCommandInterceptor class has even not been called!

My custom Interceptor:

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;
    }
}

The code to call Interceptor:

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);

Entity Framework section in configuration file:

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

Any suggestion will be appreciated!

1
0
12/15/2015 9:49:06 AM

Popular Answer

Finally, I resolved this issue. The route is that, my Context class generated by EF inherits ObjectContext.

After I rebuilt the edmx file and made the Context class inherit DbContext, the issue has gone.

1
12/16/2015 1:59:48 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow