zoukankan      html  css  js  c++  java
  • EF-记录程序自动生成并执行的sql语句日志

    在EntityFramework的CodeFirst模式中,我们想将程序自动生成的sql语句和执行过程记录到日志中,方便以后查看和分析。

    在EF的6.x版本中,在DbContext中有一个Database属性,Database.Log就是用来专门记录这种日志的。

    Database.Log是一个Action<string>委托,给其赋值一个函数就行。

    代码如下:

    using Model;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace DAL {
        public class DemoContext:DbContext {
            //使用name=EFDemo的连接字符串
            public DemoContext() : base("EFDemo") {
    
                //设置数据库初始化方式 为 迁移(更新)数据库到最新的版本
                //DemoContext 映射数据库和表 
                //DAL.Migrations.Configuration 是迁移配置
                Database.SetInitializer(new MigrateDatabaseToLatestVersion<DemoContext, DAL.Migrations.Configuration>());
                
                //将执行的sql语句记录到日志
                Database.Log = message=>Console.WriteLine("[{0}]{1}-- {2}",Thread.CurrentThread.ManagedThreadId,DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"),message.Trim());
            }
    
            //Students属性对应数据库中的Student表
            public virtual DbSet<Student> Students { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder) {
                base.OnModelCreating(modelBuilder);
            }
        }
    }

    应用程序中的代码如下:

    using (DemoContext context = new DemoContext()) {
                    //在使用模型类之前需要强制创建数据库 true:强制创建
                    context.Database.Initialize(true);
    
                    #region 新增
    
                    Student stu = new Student {
                        Name = "赵子成",
                        BirthDay = DateTime.Parse("1990-08-01"),
                        Age = 27
                    };
    
                    //新增一个Student实体,相当于在Student表中,新增一条数据
                    context.Students.Add(stu);              
    
                    //保存
                    context.SaveChanges();
                  
                    #endregion
    }

    日志记录的结果如下:

    [10]2017/07/29 16:57:28.971-- Opened connection at 2017/07/29 16:57:28 +08:00
    [10]2017/07/29 16:57:28.972-- Started transaction at 2017/07/29 16:57:28 +08:00
    [10]2017/07/29 16:57:28.977-- DECLARE @generated_keys table([ID] uniqueidentifier)
    INSERT [dbo].[Student]([Name], [BirthDay], [Age], [Address])
    OUTPUT inserted.[ID] INTO @generated_keys
    VALUES (@0, @1, @2, NULL)
    SELECT t.[ID], t.[RowVersion]
    FROM @generated_keys AS g JOIN [dbo].[Student] AS t ON g.[ID] = t.[ID]
    WHERE @@ROWCOUNT > 0
    [10]2017/07/29 16:57:28.977--
    [10]2017/07/29 16:57:28.978-- -- @0: '赵子成' (Type = String, Size = 50)
    [10]2017/07/29 16:57:28.978-- -- @1: '1990/08/01 00:00:00' (Type = DateTime2)
    [10]2017/07/29 16:57:28.978-- -- @2: '27' (Type = Int32)
    [10]2017/07/29 16:57:28.978-- -- Executing at 2017/07/29 16:57:28 +08:00
    [10]2017/07/29 16:57:28.993-- -- Completed in 14 ms with result: SqlDataReader
    [10]2017/07/29 16:57:28.993--
    [10]2017/07/29 16:57:28.995-- Committed transaction at 2017/07/29 16:57:28 +08:00
    [10]2017/07/29 16:57:28.996-- Closed connection at 2017/07/29 16:57:28 +08:00
  • 相关阅读:
    设计模式之八:外观模式(Facade)
    Python模块学习笔记— —time与datatime
    Android加载图片OOM错误解决方式
    [C#]Attribute特性(2)——方法的特性及特性参数
    [C#]Attribute特性
    [Winform]一个简单的账户管理工具
    [C#]AES加密算法实现
    [C#基础]ref和out的区别
    [Socket网络编程]一个封锁操作被对 WSACancelBlockingCall 的调用中断。
    [Socket网络编程]由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。
  • 原文地址:https://www.cnblogs.com/williamwsj/p/7256531.html
Copyright © 2011-2022 走看看