EF Core Change Tracking
As a Junior engineer, explain EF Core Change Tracking. What problem does it solve and how would you describe it in an interview?
Answers use simple, clear English.
Quick interview answer
At Junior depth: start with the problem, then mechanism, then a short example. DbContext tracks entities loaded in context.
Detailed answer
At Junior depth: start with the problem, then mechanism, then a short example. DbContext tracks entities loaded in context. SaveChanges generates INSERT/UPDATE/DELETE. AsNoTracking skips tracking for read-only queries — faster, less memory. Core: DbContext tracks entities loaded in context. SaveChanges generates INSERT/UPDATE/DELETE. AsNoTracking skips tracking for read-only queries — faster, less memory. Real-time example: Report query uses AsNoTracking(); edit form loads entity with tracking for SaveChanges update. Pros: Automatic change detection; migrations keep schema in sync. Cons: Tracking large graphs hurts performance; stale data if long-lived context. Common mistakes: Mixing tracked and detached graphs; attaching wrong state. Best practices: Short-lived DbContext per request; explicit Entry().State when needed. Audience level: Junior.
Full explanation
DbContext tracks entities loaded in context. SaveChanges generates INSERT/UPDATE/DELETE. AsNoTracking skips tracking for read-only queries — faster, less memory.
Real example & use case
Report query uses AsNoTracking(); edit form loads entity with tracking for SaveChanges update.
Pros & cons
Pros: Automatic change detection; migrations keep schema in sync. Cons: Tracking large graphs hurts performance; stale data if long-lived context.
Common mistakes
Mixing tracked and detached graphs; attaching wrong state.
Best practices
Short-lived DbContext per request; explicit Entry().State when needed.
Follow-up questions
- How would you test EF Core Change Tracking?
- What metrics prove EF Core Change Tracking is healthy in prod?
- How does EF Core Change Tracking change at 10× traffic?