Eager loading by default in the model

dsbdev 1 Reputation point
2020-12-07T17:35:52.627+00:00

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

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
698 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Wade Gausden 166 Reputation points
    2021-03-07T01:26:13.623+00:00

    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://learn.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 person found this answer helpful.

  2. Daniel Zhang-MSFT 9,621 Reputation points
    2020-12-08T06:53:20.337+00:00

    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.