zoukankan      html  css  js  c++  java
  • 【译】第40节---EF6-命令监听

    原文:http://www.entityframeworktutorial.net/entityframework6/database-command-interception.aspx

    本节,将学习如何在执行数据库命令时拦截EF。

    EF 6提供了在对数据库执行ExecuteNonQuery,ExecuteScalar,ExecuteReader操作之前和之后使用IDbCommandInterceptor拦截上下文的能力。

    首先,实现IDbCommandInterceptor,如下所示:

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

    可以在上面的代码中看到,IDbCommandInterceptor提供了六种执行方法。

    现在,需要使用配置文件或基于代码的配置来配置拦截器。

    配置文件:

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

    基于代码配置:

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

    所以现在我们可以在DbContext执行ExecuteNonQuery,ExecuteScalar和ExecuteReader时记录命令。

  • 相关阅读:
    elk工作原理
    nginx 引入外部文件
    3.1.2 视图实例演示-登录页面
    通信架构
    JAX-WS HandlerChain使用详解
    改变规则可以,前提是得有本事——北漂18年(64)
    An internal error occurred during:"Update Installed JREs".java.lang.NullPointerException
    jquery.mobile-1.4.5.min.js:3 Uncaught TypeError: Cannot set property 'mobile' of undefined
    大数据时代,百货行业信息化将如何变革?
    17.2?Replication Implementation 复制实施:
  • 原文地址:https://www.cnblogs.com/talentzemin/p/7389586.html
Copyright © 2011-2022 走看看