【译】第40节---EF6-命令监听
原文:http://www.entityframeworktutorial.net/entityframework6/database-command-interception.aspx
本节,将学习如何在执行数据库命令时拦截EF。
EF 6提供了在对数据库执行ExecuteNonQuery,ExecuteScalar,ExecuteReader操作之前和之后使用IDbCommandInterceptor拦截上下文的能力。
首先,实现IDbCommandInterceptor,如下所示:
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);
}
}
可以在上面的代码中看到,IDbCommandInterceptor提供了六种执行方法。
现在,需要使用配置文件或基于代码的配置来配置拦截器。
配置文件:
<entityFramework>
<interceptors>
<interceptor type="EF6DBFirstTutorials.EFCommandInterceptor, EF6DBFirstTutorials">
</interceptor>
</interceptors>
</entityFramework>
基于代码配置:
public class FE6CodeConfig : DbConfiguration
{
public FE6CodeConfig()
{
this.AddInterceptor(new EFCommandInterceptor());
}
}
所以现在我们可以在DbContext执行ExecuteNonQuery,ExecuteScalar和ExecuteReader时记录命令。
【译】第40节---EF6-命令监听的更多相关文章
- 10.22 tcpdump:监听网络流量
[功能说明] tcpdump命令是一个截获网络数据包的包分析工具.tcpdump可以将网络中传送的数据包的“头”完全截获下来以提供分析.它支持针对网络层.协议.主机.端口等的过滤,并支持与.或.非逻辑 ...
- Adb connect监听指定的主机和端口/Adb监听Visual Studio Emulator for Android模拟器
语法: adb connect <host>[:<port>] 使用实例: adb connect //如果连接成功则返回 connected to 说明 在使用Visual ...
- 1-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案安全篇(来看一下怎么样监听网络数据,监听电脑上位机软件的数据)
首先安装网络监听软件 运行这个软件 这个软件安装到电脑上,默认是监听咱电脑上的网络通信 咱们先监听电脑的软件的网络通信数据,然后再说怎么监听Wi-Fi和APP的软件的网络通信数据 咱就监听咱基础篇的 ...
- redis过期key监听事件
目录 redis安装 docker拉取 启动 redis 配置 命令监听 问题 程序监听 具体监听类 效果 总结 redis常用语缓存操作,但是redis功能不仅仅于此.今天我们来看看redis的ke ...
- 第二百四十四节,Bootstrap下拉菜单和滚动监听插件
Bootstrap下拉菜单和滚动监听插件 学习要点: 1.下拉菜单 2.滚动监听 本节课我们主要学习一下 Bootstrap 中的下拉菜单插件,这个插件在以组件的形式我们 已经学习过,那么现在来看看怎 ...
- oracle 监听启动、停止、查看命令
1.su oracle 然后启动监听器 1.lsnrctl start 会看到启动成功的界面; 1.lsnrctl stop 停止监听器命令. 1.lsnrctl status 查看监听器命令. ...
- lsnrctl start 命令未找到 数据库连接报错“ORA-12541: TNS: 无监听程序”
1. lsnrctl start 命令未找到 或者bash:lsnrctl:command not found. su - oralce 切换用户的时候,中间要有-,而且-的两边有空格, ...
- Zookeeper命令行操作(常用命令;客户端连接;查看znode路径;创建节点;获取znode数据,查看节点内容,设置节点内容,删除节点;监听znode事件;telnet连接zookeeper)
8.1.常用命令 启动ZK服务 bin/zkServer.sh start 查看ZK服务状态 bin/zkServer.sh status 停止ZK服务 bin/zkServer.sh stop 重启 ...
- ASP.NET Core 发布之后通过命令控制监听地址和环境变量
添加Command支持 新建一个ASP.NET Core 项目,打开Program.cs 添加下面的代码: public class Program { public static void Main ...
随机推荐
- Markdown编辑器使用说明
Markdown编辑器使用说明 #编辑器使用说明编辑器仅用来编辑文章的样式,建议在其他文档中写好内容,再复制到此处编辑样式--- ## 编辑器使用介绍—非开发者 非开发者,可以将编辑框内容全部删掉,使 ...
- maven 入门 (一)
纠结了好久,到底要不要写一份maven入门的所谓“教程”,有好几次想写一下,但是都放弃了,因为网上的太多了,而且都很好,但是现在决定了,还是要写出来,因为者有我自己的理解.所以我想写一份教程出来. 首 ...
- 阻塞队列---ArrayBlockingQueue,LinkedBlockingQueue,DelayQueue源码分析
阻塞队列和非阻塞队列阻塞队列和非阻塞队列的区别:阻塞队列可以自己阻塞,非阻塞队列不能自己阻塞,只能使用队列wait(),notify()进行队列消息传送.而阻塞队列当队列里面没有值时,会阻塞直到有值输 ...
- 委托、匿名函数到lambda表达式
在C#2.0之前就有委托了,在2.0之后又引入了匿名方法,C#3.0之后,又引入了Lambda表达式,他们三者之间的顺序是:委托->匿名表达式->Lambda表达式,微软的一步步升级,带给 ...
- GoldenGate 12.3微服务架构与传统架构的区别
随着Oracle GoldenGate 12c(12.3.0.1.0)的发布,引入了可用于复制业务数据的新架构. 多年来,这种架构有着不同的称谓,Oracle终于在最后GA发布的版本中,以“Micro ...
- 新增async
import asyncio from threading import Thread import time now = lambda: time.time() print('start',now( ...
- WINDOW 安装ImageMagick服务端和PHP的imagick插件
写在最前: windows下要注意的就是ImageMagick版本要与php扩展的Imagick能对应上,同时Imagick版本的选择也要与服务器环境以及php的情况对应上,不然就容易安装失败,我就是 ...
- php 微信支付V3 APP支付
前言:微信支付现在分为v2版和v3版,2014年9月10号之前申请的为v2版,之后申请的为v3版.V3版的微信支付没有paySignKey参数. php 微信支付类 <?php class We ...
- 翻译 Improved Word Representation Learning with Sememes
翻译 Improved Word Representation Learning with Sememes 题目 Improved Word Representation Learning with ...
- 简单的MVC与SQL Server Express LocalDB
M模式: 类,表示数据的应用程序和使用验证逻辑以强制实施针对这些数据的业务规则. V视图: 应用程序使用动态生成 HTML 响应的模板文件. C控制器: 处理传入的浏览器请求的类中检索模型数据,然后指 ...