Interception:

Here, you will learn how to intercept EF when it executes database commands.

EF 6 provides the ability to intercept the context using IDbCommandInterceptor before and after it performs the ExecuteNonQuery, ExecuteScalar, ExecuteReader operations to the database.

First, implement IDbCommandInterceptor as shown below:

  1. class EFCommandInterceptor: IDbCommandInterceptor
  2. {
  3. public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
  4. {
  5. LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
  6. }
  7.  
  8. public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
  9. {
  10. LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
  11. }
  12.  
  13. public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContextt<System.Data.Common.DbDataReader> interceptionContext)
  14. {
  15. LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
  16. }
  17.  
  18. public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
  19. {
  20. LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
  21. }
  22.  
  23. public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
  24. {
  25. LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
  26. }
  27.  
  28. public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
  29. {
  30. LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
  31. }
  32.  
  33. private void LogInfo(string command, string commandText)
  34. {
  35. Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
  36. }
  37. }

You can see in the above code that IDbCommandInterceptor provides six methods to execute.

Now, you will need to configure the interceptor either by using config file or code-based configuration.

Config file:

  1. <entityFramework>
  2. <interceptors>
  3. <interceptor type="EF6DBFirstTutorials.EFCommandInterceptor, EF6DBFirstTutorials">
  4. </interceptor>
  5. </interceptors>
  6. </entityFramework>

Code-based config:

  1. public class FE6CodeConfig : DbConfiguration
  2. {
  3. public FE6CodeConfig()
  4. {
  5. this.AddInterceptor(new EFCommandInterceptor());
  6. }
  7. }

So now, we can log commands whenever DbContext executes ExecuteNonQuery, ExecuteScalar, and ExecuteReader.

Download DB First sample project for interception demo.

Entity Framework 6.0 Tutorials(5):Command Interception的更多相关文章

  1. Entity Framework 6.0 Tutorials(1):Introduction

    以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...

  2. 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 ...

  3. Entity Framework 6.0 Tutorials(11):Download Sample Project

    Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...

  4. Entity Framework 6.0 Tutorials(10):Index Attribute

    Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...

  5. Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping

    Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...

  6. Entity Framework 6.0 Tutorials(6):Transaction support

    Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...

  7. Entity Framework 6.0 Tutorials(3):Code-based Configuration

    Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Linux环境安装nginx

    安装需要以下工具: yum -y install gcc gcc-c++ autoconf automake 模块依赖: yum -y install zlib zlib-devel openssl ...

  2. ORACLE 11G负载均衡测试

    Oracle 11g R2中,引入了SCAN(Single Client Access Name)的特性.该特性的好处在于,在数据库与客户端之间,添加了一层虚拟的服务层,就是所谓的SCAN IP以及S ...

  3. Linux添加路由

    在Linux的VM中可以添加.删除路由. 中如图的拓扑结构中需要在中间的VM上添加路由,使最左边的VM和最右边的VM实现互通. 在这台VM上需要添加IP Forwarding的功能,在操作系统中也需要 ...

  4. SERDES高速系统(一)

    在目前主流厂商的高端FPGA 中都集成了SERDES(串并收发单元)硬核,如Altera的Stratix IV GX器件族内部集成的SERDES单通道支持600Mbit/s到8.5Gbit/s数据熟率 ...

  5. Oracle用游标删除重复数据

    CREATE OR REPLACE PROCEDURE PR_MOD_BASE IS cursor c_base IS SELECT MIN(INVENTORY_DATE) IDATE,KUNNR,M ...

  6. juc线程池原理(六):jdk线程池中的设计模式

    一.jdk中默认线程池中的代理模式 单例类线程池只有一个线程,无边界队列,适合cpu密集的运算.jdk中创建线程池是通过Executors类中提供的静态的方法来创建的,其中的单例类线程池的方法如下: ...

  7. Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)

    模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...

  8. [新手教程]windows 2003 php环境搭建详细教程(转)

    对于windows服务器的php环境配置一直是是新人朋友的难题,也难倒了很多高手.这里分享一个新手教程,给那些建站新人使用.本教程来自朋友吴文辉的博客,欢迎大家有时间可以访问他的博客:吴文辉博客htt ...

  9. 1.solr学习速成之配置文件

    什么是solr Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过H ...

  10. SSD惊悚的跪了,找回数据

    1.电脑出现小bug,例如显示乱码,开机变慢等,都是SSD跪的前兆,哭,我怎么就没看出来,而且没有备份过数据. 2.终于,PC死机了...OK,重启,纳尼?起不来... 3.大哭,所有代码,所有论文不 ...