Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

Entity Framework 4.1 default eager loading

I'm using Entity Framework 4.1 code first approach.

I want to make eager loading as my the dafault configuration, and by that avoid using the Include extension method in each fetching query.

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; 
    }
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...