0

My project uses Entity Framework Core as ORM. Lazy loading is enabled by default:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseLazyLoadingProxies();
}

I need to write a code, which processes some big collection of objects with navigation properties. To decrease amount of database requests, I would like to download the objects with their navigation properties "eagerly".

What is right way to do this? Can I just use something like this:

dbContext.MyObjects.Include(myObject => myObject.NavigationProperty).ToListAsync()
1
  • 1
    Yes. This is the case when better to try first. Commented Sep 24, 2021 at 15:24

1 Answer 1

1

Yes, Include forces the EF to load the data for the given property within the query.

You might also consider to use .ToListAsync() overload and await the call. It might help the thread to not freeze while loading a huge result set from the database.

Doco with examples can be found here: https://learn.microsoft.com/en-us/ef/core/querying/related-data/eager

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.