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. spring 学习 二 IOC/DI

    中文名称:控制反转 英文名称:( Inversion of Control ) 1 控制反转作用: 一般在编写java程序时,需要程序员自己创建对象的实例,例如 A a=new A();语句,就是程序 ...

  2. 树状数组训练题2:SuperBrother打鼹鼠(vijos1512)

    先给题目链接:打鼹鼠 这道题怎么写? 很明显是树状数组. 而且,很明显是二维树状数组. 如果你没学过二维的树状数组,那么戳开这里:二维树状数组 看完以后,你就会知道怎么做了. 没有什么好解释的,几乎就 ...

  3. response设置编码格式

    response设置编码的三种方式 在java后台的Action代码或者Servlet代码中用response的方法来设置输出内容的编码方式,有以下三个方法: 1.response.setCharac ...

  4. delphi 中如何访问另一个类中到私有方法?(转载)

    原始连接 http://rvelthuis.blogspot.tw/2018/01/accessing-private-methods-of-another.html Accessing privat ...

  5. 使用 jfreechart 生成 曲线、柱状图、饼状图、分布图 展示到JSP

    虽然现在JS做报表和图形展示已经非常普遍和漂亮了,但是不能忽略有jfreechart 这样一种东西! 这些翻阅资料,在看以前写的示例时发现了关于jfreechart 的简单示例,不管怎样发上来分享一下 ...

  6. 55.UIbutton点击切换颜色

    #import "ViewController.h" #define width_w     [UIScreen mainScreen].bounds.size.width #de ...

  7. 690. Employee Importance

    好几种写法,这里贴几个出来 第一种:暴力解法,除去递归栈,空间复杂度O(1).时间复杂度略高 /* // Employee info class Employee { public: // It's ...

  8. 半透明全屏蒙层+全屏屏蔽+内容居中+css

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 同时安装python2.7和python3.5

    同时安装python2.7和python3.5,并配置sublime ctrl+B选择运行python版本 安装python 首先是安装两个版本的python,并配置相应的环境变量 1.在下载安装好P ...

  10. 视图,sql注入问题,事物,存储过程

    视图:本质是一张虚拟的表数据来自select语句用途是保障原表安全 功能1:隐藏部分数据,开放指定数据功能2:因为视图可以将查询结果保存的特性,我们可以用视图来达到减少书写sql语句的功能 例如:se ...