实体框架 4(使用过滤器配置的关联)
我有一个包含调查实体集合的用户实体。我希望关联在关系上包含一个过滤器,例如“IsCompleted”,因此每当我急切加载(或延迟加载)集合时,就会发生这种过滤。
这是我们可以控制的吗?
谢谢!
I have a user entity that contains a collection of survey entities. i would like the assocation to include a filter on the relationship, such as 'IsCompleted', so whenever i eager load (or lazy load for that matter) the collection, this filtering happens.
Is this something we have control over?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用支持视图的数据库后端,您可以考虑使用视图作为调查实体集合的源。利用数据库的力量为您进行过滤。
If you are using a DB back-end that supports views, you might consider using the view as the source for the collection of survey entities. Leverage the power of the DB to do that filtering for you.
加载实体的关联总是会获取所有关联,无论是因为您在初始查询期间使用了 Include、事后调用了 Load,还是延迟加载导致了这种情况。导航属性的概念假设了这种行为。
EJ Brennan 的答案效果很好。如果您不关心在幕后加载所有调查(由于性能/内存原因或其他原因),那么您还可以考虑通过实体上返回过滤列表的部分类定义创建一个单独的属性。
Loading of associations for an entity always just gets them all, whether because you used Include during the initial query, called Load after the fact, or lazy-loading caused it. The concept of the navigation property kind of assumes this behavior.
E.J. Brennan's answer would work well. If you're not concerned about loading all surveys behind the scenes (because of performance/memory reasons or something) then you might also consider creating a separate property via a partial class definition on your entity that returns the filtered list.