EF "Database-First" ObjectContext and DbInterception?

entity-framework entity-framework-6

Question

Is it possible to use EF 6.0's DbInterception with Database-First ObjectContext? or is it only used via DbContext?

I couldn't get it to work with my good old (legacy) ObjectContext.

Thanks in advance, Shlomi

1
2
3/20/2014 12:49:31 PM

Popular Answer

AFAIK DbInterception is independent of whether you use Database-First or Code-First modelling.

You can just add an interceptor near the start of your application.

public class LogInterceptor : IDbCommandInterceptor
{
    public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
    }

    public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        System.Diagnostics.Debug.WriteLine(command.CommandText);
    }

    public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
    {
    }

    public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
    {
        System.Diagnostics.Debug.WriteLine(command.CommandText);
    }

    public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
    }

    public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        System.Diagnostics.Debug.WriteLine(command.CommandText);
    }
}

Usage:

// Add an interceptor to log executed SQL queries.
DbInterception.Add(new LogInterceptor());
3
4/16/2014 11:52:24 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