efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Is virtual for lazy loading still needed?

Open angelsix opened this issue 11 years ago • 8 comments

I ask because the UnicornStore demo doesnt set for example Product as virtual in CarlItem and OrderLine, yet it does .Include(i => i.Product) which wouldnt be needed if it wasn't virtual lazy loaded in the first place.

angelsix avatar Oct 04 '15 08:10 angelsix

That's what I thought. If virtual is still needed then the sample code is misleading and or wrong or just superfluous as it calls .Include when a non virtual property will already be fully loaded

Sent from my Samsung Galaxy smartphone.

-------- Original message -------- From: Derek [email protected] Date: 04/10/2015 20:39 (GMT+00:00) To: aspnet/EntityFramework [email protected] Cc: Luke Malpass [email protected] Subject: Re: [EntityFramework] Is virtual for lazy loading still needed? (#3318)

This is an unofficial answer in the form of a question, but how would lazy loading be implemented without an overridden property?

Reply to this email directly or view it on GitHubhttps://github.com/aspnet/EntityFramework/issues/3318#issuecomment-145380507.

angelsix avatar Oct 04 '15 19:10 angelsix

@angelsix EF7 does not lazy-load automatically through navigation properties so that does not play as a reason to make the properties virtual.

divega avatar Oct 05 '15 06:10 divega

Ah ok. My understanding was if a single item isn't marked as virtual, it will fully load when you load the parent item. Only collections/lists didn't load automatically. So a Product would fully load, but an ICollection would not of.

Is my understanding wrong? I am sure in EF6 I had to mark single navigation properties on a class as virtual otherwise they got eager loaded when the parent object was loaded.

angelsix avatar Oct 05 '15 07:10 angelsix

What you are describing sounds a bit like how things work in NHibernate: If you specify 'lazy = false' on a property, it will get eager loaded; collections default to 'lazy = true' and references default to 'lazy = false'.

In EF6 you make navigation properties virtual to enable the creation of proxy types in which we can embed the ability to lazy load. EF6 will never eager load anything automatically (you have to ask for it in the query), and if lazy loading is enabled it will work the same for collection and reference properties.

divega avatar Oct 05 '15 12:10 divega

@rowanmiller do you know if we have anything in the documentation, feature list, roadmap or blog posts that mentions the status of lazy loading in EF7?

divega avatar Oct 05 '15 16:10 divega

It's listed on our roadmap as a high priority feature but something we'll implement based on feedback https://github.com/aspnet/EntityFramework/wiki/Roadmap#high-priority-features

rowanmiller avatar Oct 05 '15 16:10 rowanmiller

+1 for Lazy load

applenele avatar Oct 25 '15 02:10 applenele

In the past. I have used the dynamic linq (https://github.com/mrKrizz/dynamic-linq) with lazy load. but in the ef7, without the lazy load, I cannot use the dynamic linq anymore. sad. At the same time, we have to type more codes to solve circular reference. I really hope it can come back.

yukozh avatar Oct 25 '15 02:10 yukozh