dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.62k stars 1.96k forks source link

Add a note about loading multiple properties for Entry #3252

Open omuleanu opened 3 years ago

omuleanu commented 3 years ago

this works: await context.Entry(ent).Reference(o => o.Prop1).LoadAsync(); but how do you load multiple properties, you can't chain multiple .Reference


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

AndriySvyryd commented 3 years ago

@smitpatel FYI

smitpatel commented 3 years ago

For explicit loading you would need to load each navigation individually. So make similar call for each navigation with Reference/Collection API. This will cause 1 database round-trip for each loaded navigation.

If you want to load multiple navigations in single database round-trip then it is not really explicit loading, it is eager loading (load navigation(s) when querying from database). You can write query to do so like this. await context.EntitySets.Where(e => e == ent).Include(...).LoadAsync() This will avoid multiple database round-trip at the expense of fetching data for entity from server again. The entity entry in client side won't change since EF Core doesn't materialize entity from database if entity with same key is already being tracked. It just re-used already tracked instance.

If you want to remove trade-off of both above (multiple roundtrips vs fetching additional data), then you would need to write a query manually which fetches data for related navigations and mark navigations loaded explicitly in entity entry.

omuleanu commented 3 years ago

@smitpatel I ended up using DbSet.Where(...).Include(...).Include(...) what I was trying to say is that the documentation doesn't specify what to do when you want to load more than one property

smitpatel commented 3 years ago

cc: @roji

roji commented 3 years ago

Yeah, we could add a note about this.