question

dsbdev-7403 avatar image
0 Votes"
dsbdev-7403 asked ·

Eager loading by default in the model

Would like a specific entity to have a navigation property eager loaded by default.

Is this a feature in EF5?

https://github.com/dotnet/efcore/pull/19355

dotnet-entity-framework-core
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered ·

Hi dsbdev-7403,
The default behavior of an Entity Framework is Lazy Loading, where a child entity is loaded only when it is accessed for the first time. It simply delays the loading of the related data, until you ask for it.
And Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query, so that we don't need to execute a separate query for related entities. Eager loading is achieved using the Include() method.
Here is a related document you can refer to.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is the item referenced in the pull request not a way to apply eager loading in the model?


0 Votes 0 ·

Hi @dsbdev-7403,
EF doesn't have any automatic eager loading configuration as for example available in Linq-to-sql. You need to use Include (either directly or by some helper construction).
About the link you provided, this is an internal API that supports the Entity Framework Core infrastructure.
Best Regards,
Daniel Zhang

0 Votes 0 ·
WadeGausdenNZ avatar image
1 Vote"
WadeGausdenNZ answered ·

I really struggled to find documentation this a while back, but it does exist on the NavigationBuilder property. It's now called "AutoInclude". Documentation here :

https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.builders.navigationbuilder.autoinclude?view=efcore-5.0

It's used like so :

 builder.Navigation(x => x.Customer).AutoInclude();

A guide on it here incase you need to opt out too : https://dotnetcoretutorials.com/2021/03/07/eager-load-navigation-properties-by-default-in-ef-core/

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Fantastic Wade, thank you.

0 Votes 0 ·