ASP.NET Core Web API Interview Question #115

What is the difference between eager, explicit, and lazy loading?

Dependency Injection, Services, EF Core, Async & Data Access Mid-Level Intermediate

Quick Interview Answer

Eager loading retrieves related data as part of the original query. Explicit loading requests related data later through the DbContext. Lazy loading automatically queries related data when a navigation property is accessed, when configured.

Detailed Explanation

Eager loading retrieves related data as part of the original query. Explicit loading requests related data later through the DbContext. Lazy loading automatically queries related data when a navigation property is accessed, when configured.

Lazy loading can make database access difficult to see and may cause N+1 queries in APIs. Many production APIs prefer explicit query shapes through projection or deliberate eager loading.

Code Example

// Eager loading
var order = await _db.Orders
    .Include(x => x.Items)
    .SingleAsync(x => x.Id == id, token);