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:

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.

Entity Framework 6.0 Tutorials(4):Database Command Logging的更多相关文章

  1. Entity Framework 6.0 Tutorials(1):Introduction

    以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...

  2. Entity Framework 6.0 Tutorials(11):Download Sample Project

    Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...

  3. Entity Framework 6.0 Tutorials(10):Index Attribute

    Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...

  4. Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping

    Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...

  5. Entity Framework 6.0 Tutorials(6):Transaction support

    Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...

  6. Entity Framework 6.0 Tutorials(3):Code-based Configuration

    Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...

  7. Entity Framework 6.0 Tutorials(2):Async query and Save

    Async query and Save: You can take advantage of asynchronous execution of .Net 4.5 with Entity Frame ...

  8. Entity Framework 6.0 Tutorials(8):Custom Code-First Conventions

    Custom Code-First Conventions: Code-First has a set of default behaviors for the models that are ref ...

  9. Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange

    DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...

随机推荐

  1. 【LeetCode】129. Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  2. element resetFields 方法报错

    resetFields  对整个表单进行重置,将所有字段值重置为初始值并移除校验结果:如果用了之后报错如下: 查了下资料,是DOM加载的顺序问题: 解决方法: this.$nextTick(() =& ...

  3. as3随机数

    for(var i:int = 0;i<100;i++){    trace(Math.floor(Math.random()*3)); } Math.floor(Math.random()*3 ...

  4. numpy之通用函数ufunc

    通用函数-元素级数组函数 通用函数(ufunc)是一种对ndarray执行元素级运算的函数. 一元ufunc import numpy as np arr = np.arange(-10,10,2) ...

  5. java多线程实现礼花绽放的效果,

    总结:主要是那个红点点在上升的过程中要涂黑色,其实它不是一个点,是一个长条,而是被涂成黑色而隐藏了.还有这个睡眠时间,多线程 是你在面板上随便点,会出现随机的颜色圆圈,点哪里,哪里就可以出现圆 imp ...

  6. 如何判断PHP数组是否为空

    PHP判断数组为空首选方法:count($arr),size($arr); $arr=array("");echocount($arr);echo size($arr); //输出 ...

  7. Oracle 2套rac集群指向单机多实例的复制搭建

    Oracle 2套rac集群指向单机多实例的复制搭建 由于环境限制,现在需要把2套rac集群通过dg复制指向远端的单机多实例上面. rac指向第一个实例的前面已经有文档 这里直接添加第二个实例的复制搭 ...

  8. mongoDB的了解

    一.什么是mongoDB? 1.MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能. 2.MongoDB 旨在为WEB ...

  9. python's twenty day for me 继承 和 super()方法

    super(): 在单继承中就是单纯的寻找父类. 在多继承中就是根据子节点所在图 的mro顺序,找寻下一个类. 遇到多继承和super(): 对象.方法 1,找到这个对象对应的类. 2,将这个类的所有 ...

  10. selenium 对浏览器的操控 java

    driver.navigate().back();     后退 driver.navigate().forward();   前进 driver.navigate().refresh();    刷 ...