Performance 如何延迟查询集合导航属性
当我不得不跟随POCO的时候Performance 如何延迟查询集合导航属性,performance,entity-framework,linq-to-entities,Performance,Entity Framework,Linq To Entities,当我不得不跟随POCO的时候 class Poco { public int Id {get; set;} public virtual ICollection<Child> Children {get; set;} } 我想得到一些子类,如果它们满足某个谓词,例如 Poco mypoco = getMyPoco(); IEnumerable<Child> someChildren = mypoco.Children
class Poco {
public int Id {get; set;}
public virtual ICollection<Child> Children {get; set;}
}
我想得到一些子类,如果它们满足某个谓词,例如
Poco mypoco = getMyPoco();
IEnumerable<Child> someChildren = mypoco.Children
.Where( child => child.MyProperty > 30);
Poco-mypoco=getMyPoco();
IEnumerable someChildren=mypoco.Children
.Where(child=>child.MyProperty>30);
我注意到这首先从数据库中获取所有子项,然后在返回的列表中进行筛选。如何确保该条件在数据库上而不是在我的应用程序中运行?您可以使用
load
方法使用显式加载,但需要为临时设置禁用延迟加载
var context = ...; // DbContext
context.Configuration.LazyLoadingEnabled = false;
context.Entry(mypoco)
.Collection(poco => poco.Children)
.Query()
.Where(child => child.MyProperty > 30)
.Load();
该筛选将在数据库中完成,并加载到子项中
var context = ...; // DbContext
context.Configuration.LazyLoadingEnabled = false;
context.Entry(mypoco)
.Collection(poco => poco.Children)
.Query()
.Where(child => child.MyProperty > 30)
.Load();