Entity Framework Core Interview Question #38

What is eager loading in EF Core?

Relationships, Navigation Properties & Loading Data Junior Beginner

Quick Interview Answer

Eager loading retrieves related data as part of the query by using Include and ThenInclude.

Detailed Explanation

Eager loading retrieves related data as part of the query by using Include and ThenInclude.

It is useful when the application knows that related data will be needed immediately. However, including large collections or multiple collection relationships can create expensive queries and large result sets.

Use eager loading deliberately and consider projection when only a subset of related data is required.

Code Example

var orders = await db.Orders
    .Include(o => o.Customer)
    .Include(o => o.Items)
    .ToListAsync();