不使用WCF数据服务的实体框架中的查询拦截器
我正在使用 EF 4.1 Code First 方法。我不想使用 WCF 数据服务。我还可以实现查询拦截器吗?任何有关此的指示都将受到高度赞赏。谢谢!
I am using EF 4.1 Code First approach. I do NOT wish to use WCF Data services. Can I still implement Query interceptor? Any pointers regarding this will be highly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
No EF 根本不提供查询拦截器。它纯粹是 WCF 数据服务的补充。您必须为这种逻辑实现自己的基础设施,但我怀疑它在全球范围内是否可行。
您可以在您的上下文中执行类似的操作:
嗯,这不是很好,因为它将业务逻辑移动到数据访问层并且是硬编码的。更大的问题是,只有当您在上层使用
ClientsQuery
而不是直接使用Clients
时,它才有效。更糟糕的是,它仅适用于直接查询,不适用于关系。因此,如果您的Product
实体包含向所有购买过该产品的客户提供的导航属性,则该导航属性永远不会被您的条件过滤,因为 EF 根本不支持导航属性的过滤。这个问题没有通用的解决方案。您的业务逻辑必须通过在需要的地方添加正确的条件并在过滤导航属性的情况下使用投影来处理此问题,例如:
顺便说一句。 WCF 数据服务也无法解决导航属性。
No EF doesn't offer query interceptors at all. It is purely WCF Data Services addition. You must implement your own infrastructure for such logic but I have some doubts that it is doable on global level.
You can do something like this in your context:
Well, it is not very nice because it moves business logic to data access layer and it is hardcoded. The bigger problem is that it works only if you use
ClientsQuery
in your upper layer instead ofClients
directly. Even worse is that it works only for direct queries but not for relations. So if you have for exampleProduct
entity containing navigation property to all clients who ever bought the product this navigation property will never be filtered by your condition because EF doesn't support filtering of navigation properties at all.There is no general solution for this problem. You business logic must handle this by adding correct conditions where it is needed and using projections in case of filtering navigation properties like:
Btw. navigation properties are not solved by WCF Data Services as well.