简体   繁体   中英

EF6 LazyLoadingEnabled enabled but loads related objects

I use EF 6 with my database. I call constructor ctx=new EntityContext(). Then try to get ctx.Set() for example. LazyLoadingEnabled = true by default. But method returns collection with included related objects. More over, it looks like objects are recursively looped. What am I done wrong? Why lazy loading isn't work? I also start SQL profiler, I'm not much familiar with it, but... I start monitoring in profiler, then start test application and... the only request I saw is request to my table and no other requests to related tables. Request looks like:

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name], 
[Extent1].[CreateDate] AS [CreateDate], 
[Extent1].[Type] AS [Type], 
[Extent1].[Status] AS [Status]
FROM [dbo].[Task] AS [Extent1]

thats all, but debugger show entity with related objects. Why so?

I believe the issue here is not EF loading the related object, but YOU loading them!

If you're looking at a collection in the debugger in VS, then you expand a collection, EF will go and query the database for the related collection (the same as if you did it in code).

If you watch SQL profiler when you do this, you should see it go and perform a second query at that point.

EF works via dynamic proxies, and expanding a collection in the debugger is the equivalent of working with the object in code, and calling MyObject.TheCollectionProperty.ToList()

Lazy loading just means that the SQL needed to populate the related collection isn't going to be executed unless you try to access the related collection (as opposed to a single up front query with a join). Sometimes this is desirable, sometimes this isn't. It will depend on how your application logic wants to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM