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:
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的更多相关文章
- Entity Framework 6.0 Tutorials(1):Introduction
以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...
- Entity Framework 6.0 Tutorials(11):Download Sample Project
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...
- Entity Framework 6.0 Tutorials(10):Index Attribute
Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...
- Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping
Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- Entity Framework 6.0 Tutorials(3):Code-based Configuration
Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...
- 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 ...
- 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 ...
- Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange
DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...
随机推荐
- Spring IOC容器的初始化-(三)BeanDefinition的注册
---恢复内容开始--- 前言 在上一篇中有一处代码是BeanDefiniton注册的入口,我们回顾一下. 1.BeanDefiniton在IOC容器注册 首先我们回顾两点,1. 发起注册的地方:2. ...
- redis key设计技巧
把表名转换为key前缀, 第二端放置表用于区分区key的字段–对应mysql中的主键的列名如userid. 3.放置主键值,如1,2,3,…..,a,b,c. 4.放要存储的列名 user表 user ...
- ubuntu下使用code::blocks编译运行一个简单的gtk+2.0项目
在具体的操作之前,首先需要安装一些必要的软件.ubuntu下默认安装了gcc,不过缺少必要的Header file,可以在命令行中输入下面的指令安装build-essential套件:sudo apt ...
- 直接通过ADO操作Access数据库
我在<VC知识库在线杂志>第十四期和第十五期上曾发表了两篇文章——“直接通过ODBC读.写Excel表格文件”和“直接通过DAO读.写Access文件”,先后给大家介绍了ODBC和DAO两 ...
- Ubuntu 下使用 mutt 和 msmtp 发送 Gmail 邮件
参考: http://www.cnblogs.com/refrag/archive/2012/11/28/2793533.html http://www.habadog. ...
- C#一个判断子串在父串中出现的次数
/// <summary> /// 计算字符串中子串出现的次数 /// </summary> /// <param name=”str”>字符串</param ...
- 第10章 深入理解Session与Cookie
需要很多Cookie时,考虑HTTP对Cookie数量和大小的限制. 几百或更多台服务器的时候,如何解决Session在多态服务器之间共享的问题. 还有一些安全问题,如Cookie被盗,Cookie伪 ...
- AJAX流程 代码
var xml = window.XMLHttpRequest ?new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHT ...
- dubbo学习 一 dubbo概述
1,背景 1,网站刚开时候的时候可能所有的功能业务都在一个应用里面 2,当业务不断复杂,流量不断增多的时候,就需要将原先的一个应用划分成多个独立的应用. 3,当分出来的业务越来越多的时候,应用 ...
- 分布式爬虫搭建系列 之三---scrapy框架初用
第一,scrapy框架的安装 通过命令提示符进行安装(如果没有安装的话) pip install Scrapy 如果需要卸载的话使用命令为: pip uninstall Scrapy 第二,scrap ...