DBContext lazyloadingenabled set to true still loads related entities by default

╄→尐↘猪︶ㄣ 提交于 2019-12-01 17:47:40

To disable lazy loading, set LazyLoadingEnabled to false rather than true. See Lazy, Eager, and Explicit Loading of Related Data in

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

bretcj7

You have to specifically set ProxyCreationEnabled = false if you want to set LazyLoadingEnabled = true.

The test passed on what I expected. The first query returns the Drugs object and NULL for DrugEntities. Second query returns the DrugEntities since I used the Include to do the eager loading.

var queryDrug = from d in context.Drug
                                where d.Active == true
                                select d;

                var drugresults = from d in context.Drug.Include(e => e.DrugIdentities)
                                  where d.Active == true
                                  select d;

It is the very behavior of lazy loading. if you just use the property of drugs, then there will not be any sql to query DrugIdentities. if you use DrugIdentities even just watch it in debug window, then there will be sql to query DrugIdentities and each drug with a single DrugIdentities search query. You can prove the behavior through peeking the sql captured by SQL Profiler. If you want DrugIdentities to be null while querying drug, you may change the model by remove virtual key word before DrugIdentities. Then when you query drug, DrugIdentities will keep null until you load DrugIdentities to context by another query.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!