SQL Server Interview Question #285
What is eager loading in EF Core?
ASP.NET Core, EF Core & SQL Server Real-World Scenarios Senior Advanced
Detailed Explanation
Eager loading retrieves related data as part of the intended query operation, commonly using Include and ThenInclude.
It can prevent repeated lazy-loading queries, but large Include graphs can create large result sets, duplicate relational rows, and expensive joins. EF Core can use split queries in appropriate scenarios to avoid some cartesian-explosion problems.
For API and view-model queries, projection with Select is often more efficient when only a subset of columns is needed.
Code Example
var orders = await db.Orders
.Include(o => o.Customer)
.Include(o => o.OrderItems)
.ThenInclude(i => i.Product)
.ToListAsync();