Tag: 拦截器

仅将IDbInterceptor挂钩到EntityFramework DbContext一次

IDbCommandInterceptor接口没有很好的文档记录。 我只发现了一些稀缺的教程: http://www.entityframeworktutorial.net/entityframework6/database-command-interception.aspx https://msdn.microsoft.com/en-us/data/jj556606%28v=vs.113%29.aspx https://entityframework.codeplex.com/wikipage?title=Interception https://www.tutorialspoint.com/entity_framework/entity_framework_command_interception.htm https://msdn.microsoft.com/en-us/data/dn469464%28v=vs.113%29.aspx 还有一些问题: entity framework6 – 定时查询 从IDbCommandInterceptor的实现中获取DbContext 这些是我发现的挂钩建议: 1 – 静态DbInterception类: DbInterception.Add(new MyCommandInterceptor()); 2 – 在DbConfiguration类中执行上述建议 public class MyDBConfiguration : DbConfiguration { public MyDBConfiguration() { DbInterception.Add(new MyCommandInterceptor()); } } 3 – 使用配置文件: 虽然我无法弄清楚如何将DbConfiguration类挂钩到DbContext,也不知道如何将配置方法的type部分放入。 我发现的另一个例子似乎建议您编写记录器的命名空间: type=”System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework” 我注意到DataBaseLogger实现了IDisposable , IDbConfigurationInterceptor和 IDbInterceptor 。 IDbCommandInterceptor也实现了IDbInterceptor ,所以我尝试(没有成功)将其格式化为: type=”DataLayer.Logging.MyCommandInterceptor, DataLayer” 当我直接调用静态DbInterception类时,它在每次调用时都添加了另一个拦截器。 所以我的快速而肮脏的解决方案是利用静态构造函数: //This partial […]

Entity Framework 6.1.0 – 使用Command Interceptor时出错

在我的应用程序中,我执行以下操作 从数据库中读取:我从数据库中获取项目(并行,每个任务的上下文)。 写入数据库:我创建一个新的上下文,并为每个获取的项目,我将它附加到新的 上下文 我定义了一个拦截器,因为我不希望EF会保存更改(我需要自己做,后来因为性能) 问题 – 在我调用SaveChanges()方法之后,调用Interceptor并且我有SQL语句并且没关系。 但是,在拦截器端执行代码之后,Context.SaveChanges()方法有一个例外。 这样说: “存储更新,插入或删除语句影响了意外的行数(0)。实体可能已被修改或删除,因为实体已加载。刷新ObjectStateManager条目。” 有人可以帮我解决这个问题吗? 谢谢。 Hagai

从IDbCommandInterceptor的实现中获取DbContext

我正在使用IDbCommandInterceptor实现: public class MyInterceptor : IDbCommandInterceptor { public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) { var context = interceptionContext.DbContexts.FirstOrDefault(); } public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void ScalarExecuted(DbCommand command, […]