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:

  1. using (var context = new SchoolDBEntities())
  2. {
  3. context.Database.Log = Console.Write;
  4. var student = context.Students
  5. .Where(s => s.StudentName == "Student1").FirstOrDefault<Student>();
  6.  
  7. student.StudentName = "Edited Name";
  8. context.SaveChanges();
  9. }

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:

  1. public class Logger
  2. {
  3. public static void Log(string message)
  4. {
  5. Console.WriteLine("EF Message: {0} ", message);
  6. }
  7. }
  8.  
  9. class EF6Demo
  10. {
  11.  
  12. public static void DBCommandLogging()
  13. {
  14. using (var context = new SchoolDBEntities())
  15. {
  16.  
  17. context.Database.Log = Logger.Log;
  18. var student = context.Students
  19. .Where(s => s.StudentName == "Student1").FirstOrDefault<Student>();
  20.  
  21. student.StudentName = "Edited Name";
  22. context.SaveChanges();
  23. }
  24. }
  25. }

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. runtime获取对象所有属性(变量)和方法

    1.包含运行时头文件 <objc/runtime.h> 2.获取某个类的成员变量或者属性: unsigned int numIvars; //成员变量个数 Ivar *vars = cla ...

  2. 【java规则引擎】drools6.5.0中kie的概论

    什么是KIE? KIE是jBoss里面一些相关项目的统称,下图就是KIE代表的一些项目,其中我们比较熟悉的就有jBPM和Drools. 这些项目都有一定的关联关系,并且存在一些通用的API,比如说涉及 ...

  3. assembly 需要 unload 和 update 的时候怎么办?

    我正在开发公司的业务组件平台,组件池的灵活性要求很高,业务组件都是可以立即更新和及时装配的;目前完成这些功能,有待测试.用appDomain.unload 拆卸assembly 可以,只是用起来比较麻 ...

  4. ②HttpURLConnection通过Json参数方式提交Post请求

    之前的文章介绍过通过报文的方式HttpURLConnection提交post请求,今天介绍下通过Json参数的方法提交Post请求,先上代码 public static HttpResponse se ...

  5. new Date(2016,3,29,10) 时区的问题

    http://my.oschina.net/xinxingegeya/blog/394821http://www.cnblogs.com/qiuyi21/archive/2008/03/04/1089 ...

  6. Java文件压缩优化工具(ProGuard) 软件介绍 Soft content

    ProGuard是一款免费的Java类文件的压缩.优化.混肴器.它可以帮你删除没用的类,字段,方法与属性,使字节码最大程度地优化,使用简短且无意义的名字来重命名类.字段和方法 .目前eclipse已经 ...

  7. laravel中好用的支付安装包

    是包括支付宝和微信的支付 准用包,在测试中 https://github.com/yansongda/laravel-pay 这个包,看上去很好但是composer require时,要求php太高, ...

  8. nginx 安装echo模块

    学习资源: https://www.cnblogs.com/xwupiaomiao/p/7997938.html https://blog.csdn.net/hb1707/article/detail ...

  9. PTA 词频统计(30 分)

    词频统计(30 分) 请编写程序,对一段英文文本,统计其中所有不同单词的个数,以及词频最大的前10%的单词. 所谓“单词”,是指由不超过80个单词字符组成的连续字符串,但长度超过15的单词将只截取保留 ...

  10. 主流ETL工具

    主流ETL产品: Ascential公司的Datastage(Datastage在2005年被IBM收购).Informatica公司的Powercenter. NCR Teradata公司的ETL ...