问题描述
我使用的是实体框架 4.1 代码优先方法.
I'm using Entity Framework 4.1 code first approach.
我想将预先加载作为我的 dafault 配置,从而避免在每个获取查询中使用 Include 扩展方法.
I want to make eager loading as my the dafault configuration, and by that avoid using the Include extension method in each fetching query.
我按照 MSDN 中的建议做了,更改了 DbContext 构造函数中的简单惰性属性:
I did as recomended in MSDN, changing the simple lazy property at the DbContext constructor:
public class EMarketContext : DbContext
{
public EMarketContext()
{
// Change the default lazy loading to eager loading
this.Configuration.LazyLoadingEnabled = false;
}
}
不幸的是,这种方法不起作用.我必须使用 Include 方法在每个查询中执行预先加载.任何想法为什么?提前致谢.
unfortunately, this approach is not working. I have to use the Include method to perform eager loading in each query. Any ideas why?Thanks in advance.
推荐答案
预加载没有默认配置.您必须始终定义 Include
或创建一些可重用的方法来包装添加包含.例如,您可以将类似的方法放置到您的上下文中:
There is no default configuration for eager loading. You must always define Include
or create some reusable method which will wrap adding include. For example you can place similar method to your context:
public IQueryable<MyEntity> GetMyEntities()
{
return this.MyEntities.Include(e => e.SomeOtherEntities);
}
这篇关于Entity Framework 4.1 默认预加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!