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. RK3288 模块单独编译

    模块以Email为例: 1.执行build目录下的脚本文件envsetup.sh $ source ./build/envsetup.sh 2.选择版本(user为用户版本   eng为工程版本) $ ...

  2. java代码实现点击鼠标从控制台输出信息

    总结:最难的就是当我们需要点击按钮时去实现某个功能-----------因为那个我没有理解透,是涉及整个程序的 package com.a.b; import javax.swing.*; impor ...

  3. Vue.js:目标结构

    ylbtech-Vue.js:目标结构 1.返回顶部 1. Vue.js 目录结构 上一章节中我们使用了 npm 安装项目,我们在 IDE(Eclipse.Atom等) 中打开该目录,结构如下所示: ...

  4. 表空间 -- tablespace

    表空间是数据库的逻辑划分,一个表空间只能属于一个数据库.所有的数据库对象都存放在指定的表空间中.但主要存放的是表, 所以称作表空间. Oracle数据库中至少存在一个表空间,即SYSTEM的表空间. ...

  5. Three.js会飞走飞回来的鸟

    效果图 demo import './index.css'; // stats var stats; (function(){ stats = new Stats(); document.body.a ...

  6. Java之IO输入输出

    首先介绍File类: 我们直接上代码: package com.learn.chap10.sec02; import java.io.File; import java.io.IOException; ...

  7. quarz入门案例

    介绍 Quartz框架是一个全功能.开源的任务调度服务,可以集成几乎任何的java应用程序—从小的单片机系统到大型的电子商务系统.Quartz可以执行上千上万的任务调度.   核心概念  Quartz ...

  8. PYTHON 中的字符集

    一.前言 Python中的字符编码是个老生常谈的话题,今天来梳理一下相关知识,希望给其他人些许帮助. Python2的 默认编码 是ASCII,不能识别中文字符,需要显式指定字符编码:Python3的 ...

  9. [故障及解决]SoundPool没有声音

    问题描述:使用SoundPool类进行播放声音时,在手机上没有声音. 问题代码: /** * 声音播放 */ private void playSound() { SoundPool soundPoo ...

  10. c# 通过dllimport 调用c 动态链接库

    https://blog.csdn.net/zhunju0089/article/details/80906501 这篇文件很详细 讲述了如何创建c 动态链接库项目 有一些注意的地方 不做介绍 下面是 ...