Entity Framework 6.0 Tutorials(5):Command Interception
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:
class EFCommandInterceptor: IDbCommandInterceptor
{
public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
} public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
} public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContextt<System.Data.Common.DbDataReader> interceptionContext)
{
LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
} public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
} public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
} public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
} private void LogInfo(string command, string commandText)
{
Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
}
}
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:
<entityFramework>
<interceptors>
<interceptor type="EF6DBFirstTutorials.EFCommandInterceptor, EF6DBFirstTutorials">
</interceptor>
</interceptors>
</entityFramework>
Code-based config:
public class FE6CodeConfig : DbConfiguration
{
public FE6CodeConfig()
{
this.AddInterceptor(new EFCommandInterceptor());
}
}
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的更多相关文章
- Entity Framework 6.0 Tutorials(1):Introduction
		
以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...
 - 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 ...
 - Entity Framework 6.0 Tutorials(11):Download Sample Project
		
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...
 - Entity Framework 6.0 Tutorials(10):Index Attribute
		
Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...
 - Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping
		
Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...
 - Entity Framework 6.0 Tutorials(6):Transaction support
		
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
 - Entity Framework 6.0 Tutorials(3):Code-based Configuration
		
Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...
 - 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 ...
 - 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 ...
 
随机推荐
- 解决direct2d拖拽窗口闪烁
			
响应WM_ERASEBKGND,在OnEraseBkgnd()处返回FALSE,阻止GDI重绘客户区背景色,设置背景色的工作交给Direct2D在Render时设置,否则在Resize时会出现窗口闪烁 ...
 - jQuery火箭图标返回顶部代码
			
在网上找来段使用jQuery火箭图标返回顶部代码,感觉比较酷,比较炫,大概样式如下, 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. ...
 - 开发沉思录 - 记大数据平台的一次 BUG井喷
			
研究REST提交重启:终于提交成功了,首先一个错误:地址IP地址的http://hdp0:8080/api/v1/clusters/HDP/requests ,被我错误写成了request,当误了大量 ...
 - 洛谷 1291 [SHOI2002]百事世界杯之旅
			
题目:https://www.luogu.org/problemnew/show/P1291 大水题!套路!模板! 稍微注意一下输出就行了. #include<iostream> #inc ...
 - easyui的datagird动态设置当前页数
			
if (ishas) { $("#tg").datagrid("options").pageNumber = 1; $('#tg').datagrid('rel ...
 - Uploadify在asp.net下使用Demo
			
为了使自己以后不再去网上搜索,特记录下来 从uploadify官网http://www.uploadify.com/上下载文件 必要的文件: 1.jquery的js文件 2.jquery.upload ...
 - Mysql参见SHOW命令总结
			
Mysql参见SHOW命令总结 MySQL Show命令的用法大全
 - Linux学习笔记 -- Shell 数组
			
定义 在Shell的世界里,我们只能定义一维数组. 定义数组的时候不需要指定长度,数组的下标从0开始; Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下: sh ...
 - catkin 工作空间 - Package 组成
			
package 是 ROS 软件的基本组织形式,ROS 就是由一个个的 package 组成的 package 是 catkin 的编译基本单元 一个 package 可以包含多个可执行文件(节点) ...
 - mongodb基本操作和在springboot中的使用
			
本文介绍mongodb的使用 说明 起步 mongo通用类型 mongoshell的操作 CRUD操作 shell命令操作 索引操作 mongo在springboot中的使用 目录结构 依赖 prop ...