Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way to do this.

Add a class named MyCommandInterceptor

class MyCommandInterceptor: DbCommandInterceptor
{
public override void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
base.NonQueryExecuted(command, interceptionContext);
Console.WriteLine(command.CommandText);
} public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
base.ReaderExecuted(command, interceptionContext);
Console.WriteLine(command.CommandText);
} public override void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
base.ScalarExecuted(command, interceptionContext);
Console.WriteLine(command.CommandText);
}
}

The namespace System.Data.Entity.Infrastructure.Interception is needed. Then Add DbInterception.Add(new MyCommandInterceptor()); in your constructor of DbContext class:

public class MyDb:DbContext
{
public MyDb():base("name=TestDb")
{
DbInterception.Add(new MyCommandInterceptor());
} public IDbSet<User> Users { get; set; }
}

It's all.

Lerning Entity Framework 6 ------ Using a commandInterceptor的更多相关文章

  1. Lerning Entity Framework 6 ------ Defining Relationships

    There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many Th ...

  2. Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database

    The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two ...

  3. Lerning Entity Framework 6 ------ Working with in-memory data

    Sometimes, you need to find some data in an existing context instead of the database. By befault, En ...

  4. Lerning Entity Framework 6 ------ Inserting, Querying, Updating, and Deleting Data

    Creating Entities First of all, Let's create some entities to have a test. Create a project Add foll ...

  5. Lerning Entity Framework 6 ------ Defining the Database Structure

    There are three ways to define the database structure by Entity Framework API. They are: Attributes ...

  6. Lerning Entity Framework 6 ------ Introduction to TPH

    Sometimes, you have created two models. They have the same parent class like this: public class Pers ...

  7. Lerning Entity Framework 6 ------ Complex types

    Complex types are classes that map to a subset of columns of a table.They don't contains key. They a ...

  8. Lerning Entity Framework 6 ------ A demo of using Entity framework with MySql

    Create a new project named MySqlTest Install following packages by right-clicking on the References ...

  9. Lerning Entity Framework 6 ------ Joins and Left outer Joins

    Joins allow developers to combine data from multiple tables into a sigle query. Let's have a look at ...

随机推荐

  1. CentOS7安装ms8可能出现的错误

    参照<在Centos上安装MS8.0的详细过程>:http://muchong.com/html/201507/9145663.html 安装过程中如果库文件不全,会异常终止.报错情况如下 ...

  2. 【RabbitMQ】 RabbitMQ安装

    MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序之间 ...

  3. 单片机一种简便的printf调试方案。

    此处引用csdn博客.链接如下. http://blog.csdn.net/cp1300/article/details/7773239 http://blog.csdn.net/aobai219/a ...

  4. kbmmw 中的日期时间操作

    为了精确度反映时间及时区,kbmmw 里面专门有一个单元处理日期时间,由于很多同学习惯了delphi 自带的Tdatetime,使用这个时会有一些疑惑,因此今天就单独说一下这个. 首先kbmmwdat ...

  5. 创建视图&新建表按照视图结构

    create  view  V_tableTemp as  select a.* from TEMPCLIENT a ,TEMPCLIENTSTUFF b where a.CORNO<>' ...

  6. Java的函数重载必须满足的条件

    1.函数名相同 2.参数个数不同或者参数类型不同 3.函数重载和返回值类型无关 //函数的重载 public static void get() { System.out.println(" ...

  7. boost-容器

    1.array array相当于是一个增加了STL容器接口的数组,但它不像vector等容器一样可以动态增长,如果需要动态变动array的容量可以使用boost::scoped_array.array ...

  8. 华为机试-iNOC产品部-杨辉三角的变形

    题目描述 1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 11 4 10 16 19 16 10 4 1以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数,左上角数 ...

  9. Html5与Css3知识点拾遗(一)

    1.元素 空元素: 可选的空格空格和斜杠 <img src="x.jpg" width="300" alt="pic" /> & ...

  10. 运行Xcode时,提示:An error was encountered while running (Domain = FBSOpenApplicationErrorDomain, Code = 4)

    运行Xcode模拟器时,提示: An error was encountered while running (Domain = FBSOpenApplicationErrorDomain, Code ...