C# Interview Question #174

What is eager, explicit, and lazy loading in EF Core?

EF Core, Data Access & Performance Senior Advanced

Quick Interview Answer

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

Detailed Explanation

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

Explicit loading retrieves related data later through the DbContext entry APIs when the application deliberately requests it.

Lazy loading automatically retrieves navigation data when a navigation property is accessed, but it requires configuration such as proxies or appropriate patterns. Lazy loading can make database access less visible and can easily cause N+1 query problems.

Production applications should choose loading behavior intentionally and often prefer projection when only specific related fields are needed.

Code Example

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