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

Add a class named MyCommandInterceptor

  1. class MyCommandInterceptor: DbCommandInterceptor
  2. {
  3. public override void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
  4. {
  5. base.NonQueryExecuted(command, interceptionContext);
  6. Console.WriteLine(command.CommandText);
  7. }
  8. public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
  9. {
  10. base.ReaderExecuted(command, interceptionContext);
  11. Console.WriteLine(command.CommandText);
  12. }
  13. public override void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
  14. {
  15. base.ScalarExecuted(command, interceptionContext);
  16. Console.WriteLine(command.CommandText);
  17. }
  18. }

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

  1. public class MyDb:DbContext
  2. {
  3. public MyDb():base("name=TestDb")
  4. {
  5. DbInterception.Add(new MyCommandInterceptor());
  6. }
  7. public IDbSet<User> Users { get; set; }
  8. }

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. solr搜索配置权重

    配置权重 <requestHandler name="/browse" class="solr.SearchHandler" default=" ...

  2. python输出格式对齐问题

    采用.format打印输出时,可以定义输出字符串的输出宽度,在 ':' 后传入一个整数, 可以保证该域至少有这么多的宽度. 用于美化表格时很有用. >>> table = {'Goo ...

  3. javascript对象的属性,方法,prototype作用范围分析.

    用了javascript这么久由于没有系统学习过基础,总是拿来主义. 所以对一些基础知识还是搞不清楚很混乱. 今天自己做个小例子,希望彻底能搞清楚. 注释中对象只例子的对象本身,原型只原型继承对象的新 ...

  4. [C#.net]WinForm载入窗体完成后自动执行事件

    一.以下是网络上可搜索到的次序 当 Windows Form 应用程序启动时,会以下列顺序引发主要表单的启动事件:        System.Windows.Forms.Control.Handle ...

  5. NOIP2017普及组T2题解

    还是神奇的链接 上面依然是题目. 这道题依然很简单,比起2015年的普及组t2好像还是更水一些. 不过这道题能讲的比第一题多. 我们一起来看一下吧! 这一题,我们首先将书的编号全部读入,存在一个数组里 ...

  6. 5. Longest Palindromic Substring - Unsolved

    https://leetcode.com/problems/longest-palindromic-substring/#/description Given a string s, find the ...

  7. 使用delphi-cross-socket 开发kbmmw smart http service

    前几天我说了使用delphi-cross-socket 扩展kbmmw 的跨平台支持,今天我说一下使用 kbmMWCrossScoketHttpServerTransport 在linux 下支持 k ...

  8. centos7修改root根目录

    1.拷贝/root 原目录的东西到新目录中(包括.xxx文件) /abc 2.修改配置文件 vi /etc/passwd root:x:0:0:root:/root:/bin/bash ==> ...

  9. 手机上输入http://192.168.1.102:8888/FiddlerRoot.cer为什么下载不了证书

    因为之前你的手机可能已经安装了该证书,所以再次下载会说找不到证书 解决办法:如果你遇到上面的问题,就可能是证书的问题(我的本地证书是用系统生成证书的一个软件生成的个人证书,所以出现了问题),操作步骤如 ...

  10. vim 查找替换批量替换

    一.vi查找:    当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢?在vi里可没有菜单-〉查找, 不过没关系,你在命令模式下敲斜杆(/)这时在状态栏(也就是屏幕左下脚)就出 ...