zoukankan      html  css  js  c++  java
  • Entity Framework 6.0 Tutorials(4):Database Command Logging

    Database Command Logging:

    In this section, you will learn how to log commands & queries sent to the database by Entity Framework.

    Prior to EF 6, we used the database tracing tool or third party tracing utility to trace database queries and commands sent by Entity Framework. Now, EF 6 provides a simple mechanism to log everything that Entity Framework is doing. It logs all the activity performed by EF using context.database.Log

    You can attach any method of any class, which accepts one string parameter and returns void.

    In the following example, we use Console.Write method to log EF activities:

    using (var context = new SchoolDBEntities())
    {
        context.Database.Log = Console.Write;
        var student = context.Students
                            .Where(s => s.StudentName == "Student1").FirstOrDefault<Student>();
    
        student.StudentName = "Edited Name";
        context.SaveChanges();
    }

    Output:

    database loggin output

    You can see in the output that it logs all the activities performed by EF, e.g. opening & closing connection, execution & completion time and database queries & commands.

    Context.Database.Log is an Action<string> so that you can attach any method which has one string parameter and void return type. For example:

    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 student = context.Students
                                    .Where(s => s.StudentName == "Student1").FirstOrDefault<Student>();
    
                    student.StudentName = "Edited Name";
                    context.SaveChanges();
                }
            }
    }

    Download DB First sample project for DB command logging demo.

  • 相关阅读:
    unity xcode debug 学习
    Amplify Shader Editor 学习
    Unity Quality Setting 学习
    Unity3D Distortion 学习
    【原】博客园第三方客户端-i博客园App开源
    【AR实验室】mulberryAR:并行提取ORB特征
    【AR实验室】mulberryAR :添加连续图像作为输入
    【原】C++11并行计算 — 数组求和
    【AR实验室】mulberryAR : ORBSLAM2+VVSION
    【AR实验室】OpenGL ES绘制相机(OpenGL ES 1.0版本)
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649531.html
Copyright © 2011-2022 走看看