zoukankan      html  css  js  c++  java
  • Entity Framework 6.0 Tutorials(5):Command Interception

    Interception:

    Here, you will learn how to intercept EF when it executes database commands.

    EF 6 provides the ability to intercept the context using IDbCommandInterceptor before and after it performs the ExecuteNonQuery, ExecuteScalar, ExecuteReader operations to the database.

    First, implement IDbCommandInterceptor as shown below:

    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, DbCommandInterceptionContextt<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)
        {
            Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
        }
    }

    You can see in the above code that IDbCommandInterceptor provides six methods to execute.

    Now, you will need to configure the interceptor either by using config file or code-based configuration.

    Config file:

    <entityFramework>
        <interceptors>
            <interceptor type="EF6DBFirstTutorials.EFCommandInterceptor, EF6DBFirstTutorials">
            </interceptor>
        </interceptors>
    </entityFramework>

    Code-based config:

    public class FE6CodeConfig : DbConfiguration
    {
            public FE6CodeConfig()
            {
                this.AddInterceptor(new EFCommandInterceptor());
            }
    }

    So now, we can log commands whenever DbContext executes ExecuteNonQuery, ExecuteScalar, and ExecuteReader.

    Download DB First sample project for interception demo.

  • 相关阅读:
    《C程序设计语言》练习1-10
    《C程序设计语言》练习 1-8,1-9
    被这个C程序折腾死了
    《C程序设计语言》练习 1-6,1-7
    利用圆解一元二次方程
    三角插值的 Fourier 系数推导
    利用离散 Fourier 变换解一元二次方程
    关于selenium IDE找不到元素
    【★】深入BGP原理和思想【第一部】
    【★】深入BGP原理和思想【第一部】
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649541.html
Copyright © 2011-2022 走看看