Eager vs Explicit Loading
What common mistakes do candidates make around Eager vs Explicit Loading, and how do you avoid them?
Answers use simple, clear English.
Quick interview answer
Common mistakes: Lazy loading enabled globally causing hidden N+1 in foreach. Best practices: Prefer Select projection DTOs; AsSplitQuery for multiple collections.
Detailed answer
Common mistakes: Lazy loading enabled globally causing hidden N+1 in foreach. Best practices: Prefer Select projection DTOs; AsSplitQuery for multiple collections. Core: Include/ThenInclude eager-loads related data in one query. Explicit loading: reference/load after query. Avoid lazy loading in web apps (N+1 in loops). Real-time example: Order list with items: .Include(o => o.Items) prevents N+1 when rendering line counts. Pros: Include gives predictable SQL; projection Select reduces over-fetching. Cons: Cartesian explosion with multiple Includes — use split queries. Common mistakes: Lazy loading enabled globally causing hidden N+1 in foreach. Best practices: Prefer Select projection DTOs; AsSplitQuery for multiple collections. Audience level: Fresher.
Full explanation
Include/ThenInclude eager-loads related data in one query. Explicit loading: reference/load after query. Avoid lazy loading in web apps (N+1 in loops).
Real example & use case
Order list with items: .Include(o => o.Items) prevents N+1 when rendering line counts.
Pros & cons
Pros: Include gives predictable SQL; projection Select reduces over-fetching. Cons: Cartesian explosion with multiple Includes — use split queries.
Common mistakes
Lazy loading enabled globally causing hidden N+1 in foreach.
Best practices
Prefer Select projection DTOs; AsSplitQuery for multiple collections.
Follow-up questions
Open one as its own read / solve / listen card