在实体框架中添加和删除拦截器不起作用 [英] Adding and Removing interceptors in Entity framework doesn't work

查看:75
本文介绍了在实体框架中添加和删除拦截器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用拦截器仅对下面的代码执行一个查询.

I tried using interceptor for just one query with the below code.

 var stringTrimmerInterceptor = new StringTrimmerInterceptor();
    DbInterception.Add(stringTrimmerInterceptor);
//EF query to list
  DbInterception.Remove(stringTrimmerInterceptor);   // This is not removing.

删除后,我认为拦截器将退订.但是它不是.

When it is removed, I thought the interceptor will be unsubscribed . However its' not. Can Someone help.

推荐答案

您需要一个在拦截器之前".禁止执行"被拦截的操作.也就是说,拦截器可以告诉EF 不是执行操作,而是返回其他结果.这是通过在之前"拦截器中的拦截上下文上设置Result属性来完成的.

You need a "before interceptor" to suppress execution of the operation that is being intercepted. That is, the interceptor can tell EF not to execute the operation, but to instead return some other result. This is done by setting the Result property on interception context in the "before" interceptor.

此处有更多信息:

如何我禁止从Entity Framework 6 IDbCommandTreeInterceptor执行?


这篇关于在实体框架中添加和删除拦截器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆