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. centos6.5 安装nginx

    安装之前先安装VMware tools(方便于从windows上拷贝文件到linux) 1. nginx安装环境 nginx是C语言开发,建议在linux上运行,本次使用Centos6.5作为安装环境 ...

  2. 十二、python沉淀之路--内置函数

    1.abs函数,求绝对值. a = abs(-3) print(a) 返回:3 2.all函数:判断是否是可迭代对象. 官方解释:Return True if bool(x) is True for ...

  3. Swift app中的Crash捕获与处理

    1. 为什么会Crash 常见的Crash原因有:访问已经被释放的内存,数组越界,使用!解包值为nil的变量.当遇到这些情况时,说明应用已经遇到了很严重的非预期错误,无法再继续运行.操作系统检测到这些 ...

  4. Qt5.4中遇到找不到头文件<QApplication>等。

    从新学习Qt时,重装了Qt5.4,当运行Hello World例子时,遇到了下列的情况 <span style="font-size:18px;">#include & ...

  5. MongoDB Data Model 浅谈

    MongoDB 对于数据的 schema 要求很灵活. 与 MySQL 相比,collection 并不会强制文档的结构.(MySQL 在定义表时, 需要指定有哪些字段.类型.展示长度等) 因此,插入 ...

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

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

  7. chrome浏览器手动添加印象笔记剪藏插件

    标签(空格分隔): chrome浏览器,日常办公 一直为每次从网页上复制内容之后,还需要再去复制对应的网址,倍感麻烦.之前偶尔这样操作还可以,最近在学习新东西,要保留下来的网页实在太多,而且不利于分类 ...

  8. thinkphp中的session的使用和理解!

    session的作用:session可以长时间的保存数据,不丢失. session的常用于: 1.登录,保存登录信息 2.保存购物车信息 3.保存验证码信息 定义session常量 define('W ...

  9. oracle分布式事务总结

    基本概念 Local Coordinator:在分布事务中,必须参考其它节点上的数据才能完成自己这部分操作的站点. Global Coordinator:分布事务的发起者,负责协调这个分布事务. Co ...

  10. win10系统怎么关闭自动更新

    现在win10已经很普遍了,对于win10 现在还不是很完美,比如自动更新功能,现在的选项中没有关闭自动更新的选项了,这是一个bug,微软要强制更新.我就忍受不了自动更新,会拉取网络,影响我们的上网体 ...