zoukankan      html  css  js  c++  java
  • Entity Framework入门教程(17)---记录和拦截数据库命令

    记录和拦截数据库命令

    这一节介绍EF6怎么记录和拦截发送给数据库的查询和操作命令。

    1.记录EF发送给数据库命令(DbContext.Database.Log)

    以前给了查看EF发送给数据库的命令我们需要借助数据库的追踪工具或者第三方追踪工具,现在EF6中提供了DbContext.Database.Log属性(Action<string>类型),使用这个属性我们可以很方便地记录EF发送给数据库的命令。

    下边是一个栗子:

            static void Main(string[] args)
            {
                using (EFDbContext context=new EFDbContext())
                {
                    context.Database.Log = Console.WriteLine;
                    var std1 = context.Students.Find(1);
                    std1.Name = "newName";
                    context.SaveChanges();
                    Console.ReadKey();
                }
               
            }

    输出如下:

    在上边的栗子中,Console.Write()方法属于Action<string>类型,所以可以赋值给Log属性。可以看到EF打开和关闭数据库,执行查询,和使用事务进行CUD都会被记录下来。

    我们也可以自定义一个Action<string>委托的实例赋值给Log属性:

    public class Logger
    {
        public static void Log(string message)
        {
            Console.WriteLine("EF Message: {0} ", message);
        }
    }
    
    class EF6Demo
    {
        public static void DBCommandLogging()
        {
            using (var context = new SchoolDBEntities())
            {
                    
                context.Database.Log =  Logger.Log;                
                var std1 = context.Students.Find(1);
                std1.Name = "newName";
                context.SaveChanges();
                Console.ReadKey();
            }
        }
    }

     2.拦截EF生成的数据库命令(IDbCommandIntercepter)

    EF6提供了拦截数据库的接口IDbCommandIntercepter,这个接口提供了拦截EF发送给数据库的命令的方法,我们也可以使用这个接口实现在context的操作执行前或执行后去做一些自定义的操作(类似mvc/api中的filter)。因为DbContext执行操作的底层实现是利用ADO.NET进行ExecuteNonQueryExecuteScalar, 和ExecuteReader,所以我们可以通过如NonQueryExecuted、NonQueryExecuting等方法进行拦截。

    为了实现SQL命令拦截我们首先要定义一个实现IDbCommandIntercepter接口的类:

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

    可以看出,IDbCommandInterceptor 接口提供了6个方法,分别用于在ADO.NET执行ExecuteNonQuery(),ExcuteReader(),ExcuteScalar()方法的执行前/后拦截命令。这个栗子目的是:记录context的操作是否是异步和发送到数据库的命令。我们也可以使用这些方法来实现自定义逻辑(和filter简直一模一样有木有)。

    接下来把拦截器添加到配置中去,两种实现方式:

    ① 通过配置文件,在app.config或web.config中添加如下节点

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

    ②代码配置

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

    配置完成我们就可以记录EF发送给数据库的命令了,一个栗子:

    var newStudent =  new Student() { FirstName = "Bill" };
    
    using (var context = new SchoolDBEntities())
    {
        context.Students.Add(newStudent);
        context.SaveChanges();
    }

    栗子输出为:

    Intercepted on: ReaderExecuting :- IsAsync: False, Command Text: INSERT [dbo].[Student]([FirstName], [StandardId], [LastName])
    VALUES (@0, NULL, NULL)
    SELECT [StudentID], [RowVersion] FROM [dbo].[Student]
    WHERE @@ROWCOUNT > 0 AND [StudentID] = scope_identity()
    Intercepted on: ReaderExecuted :- IsAsync: False, Command Text: INSERT [dbo].[Student]([FirstName], [StandardId], [LastName])
    VALUES (@0, NULL, NULL)
    SELECT [StudentID], [RowVersion] FROM [dbo].[Student]
    WHERE @@ROWCOUNT > 0 AND [StudentID] = scope_identity()

    EF系列目录链接:Entity Franmework系列教程汇总

  • 相关阅读:
    React 构建方法总结
    集思广益 (一)
    C#方法调用
    C# Hello World 实例
    C# 环境
    C# 简介
    对象类型的本地写入---plist文件创建以及读取
    正则判断 手机邮箱的正确格式
    数组去重
    Base64编码
  • 原文地址:https://www.cnblogs.com/wyy1234/p/9642131.html
Copyright © 2011-2022 走看看