Entity Framework Core Interview Question #101
What is change tracking in EF Core?
Change Tracking, Updates, Transactions & Concurrency Mid-Level Intermediate
Quick Interview Answer
Change tracking is EF Core's mechanism for monitoring entity instances and their state so it can determine which INSERT, UPDATE, or DELETE commands are required when SaveChanges is called.
Detailed Explanation
Change tracking is EF Core's mechanism for monitoring entity instances and their state so it can determine which INSERT, UPDATE, or DELETE commands are required when SaveChanges is called.
A tracked entity has an EntityState such as Added, Unchanged, Modified, Deleted, or Detached. EF Core also maintains relationship and property information needed to persist changes.
Tracking is valuable for update workflows, but it consumes memory and processing. Read-only queries often benefit from AsNoTracking.
Code Example
var product = await db.Products.FindAsync(id);
product!.Name = "Updated";
Console.WriteLine(db.Entry(product).State);
// Modified after change detection
await db.SaveChangesAsync();