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 ...
随机推荐
- LeetCode 621. Task Scheduler
原题链接在这里:https://leetcode.com/problems/task-scheduler/description/ 题目: Given a char array representin ...
- C#结构体数组间的转化
转自:http://developer.51cto.com/art/200908/143779.htm 解决C#结构体数组间的转化问题的由来:在写C#TCP通信程序时,发送数据时,如果是和VC6.0等 ...
- java 并发runable,callable,future,futureTask
转载自:http://www.cnblogs.com/dolphin0520/p/3949310.html package future_call; import java.util.concurre ...
- python 不同版本下载资源
Unofficial Windows Binaries for Python Extension Packages by Christoph Gohlke, Laboratory for Fluore ...
- Java 程序员容易犯的10个SQL错误
Java程序员编程时需要混合面向对象思维和一般命令式编程的方法,能否完美的将两者结合起来完全得依靠编程人员的水准: 技能(任何人都能容易学会命令式编程) 模式(有些人用“模式-模式”,举个例子,模式可 ...
- Linux 安装交叉编译工具链
交叉编译工具链下载地址: 链接:http://pan.baidu.com/s/1dE7P9rb 密码:300i 声明:下面每一步中的“pwd”指令都是为了看清楚当前的目录,没有其他实际意义. 系统:u ...
- PHP面向对象深入研究之【高级特性】
静态属性 <?php class StaticExample { static public $aNum = 0; // 静态共有属性 static public function sayHel ...
- C语言的第二天-比较大小的小程序
#include <stdio.h> int main() { int a,b,c,max; printf("请输入三个数:"); scanf("%d,%d, ...
- Java微信公众平台开发(十三)--微信JSSDK中Config配置
转自:http://www.cuiyongzhi.com/post/57.html 前端开发工程师和关注前端开发的开发者们在2015年中肯定被腾讯的JSSDk引爆过,搞APP的.搞前端的甚至是是搞后端 ...
- html收藏
全屏显示<input type="button" name="fullscreen" value="全屏显示" onclick=&qu ...