本文介绍了实体框架代码首先软删除延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 所以我使用Entity框架代码第一(所以没有.edmx)我有一个基本实体类与bool IsEnabled做软删除So I'm using Entity Framework Code First (so no .edmx) I have a base entity class with a bool IsEnabled to do soft delete's我使用存储库模式,所以对存储库的所有查询可以过滤IsEnabled。I am using repository pattern so all queries against the repository can be filtered out with IsEnabled.然而,任何时候我使用存储库获取一个MyType是IsEnabled,Lazy加载MyType.Items可能意味着不能启用Items。However any time I use the repository to get an MyType which is IsEnabled, Lazy Loading MyType.Items may mean that Items could be not enabled.有没有办法,也许用EF Fluent来描述如何对表进行过滤?Is there a way, perhaps with EF Fluent to describe how to do filtering on tables?更新:如果我有一个Dbset If I have a Dbsetpublic class UnitOfWork : DbContext {private IDbSet<MyObj> _MyObj;public IDbSet<MyObj> MyObjs { get { return _MyObj ?? (_MyObj = base.Set<MyObj>()); } }}有什么方法可以告诉DbContext过滤DbSet?Is there any way I can tell the DbContext to filter the DbSet?推荐答案不,没有办法为延迟加载定义一个过滤器code> Include )。如果您希望导航集合仅填充 IsEnabled 为 true 的项目,例如显式加载:No, there is no way to define a filter for lazy loading (also not for eager loading using Include). If you want that your navigation collections only get populated with items where IsEnabled is true you can only shape your queries accordingly, for example with explicit loading:context.Entry(parent).Collection(p => p.Items).Query() .Where(i => i.IsEnabled) .Load();这将填充 Items 编辑:我感觉有点像一个失败的战斗的坏消息的使者,他失去了他的头。也许太难以相信Entity Framework有时没有你想要的功能。为了提高我说服您的机会,我添加了一个当局的报价,Julie Lerman :I feel a bit like the messenger of the bad news about a lost battle who gets his head knocked off. Maybe it's too hard to believe that Entity Framework sometimes does not have the capabilities you want. To improve my chance to convince you I add a quote from an authority, Julie Lerman: 在实体框架中既不急切加载也不延迟/延迟加载允许您过滤或排序相关数据为返回。 这篇关于实体框架代码首先软删除延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 04:29