C# Interview Question #172
What is change tracking in EF Core?
EF Core, Data Access & Performance Senior Advanced
Quick Interview Answer
Change tracking allows EF Core to monitor entity instances and determine what INSERT, UPDATE, or DELETE operations are needed when SaveChanges is called.
Detailed Explanation
Change tracking allows EF Core to monitor entity instances and determine what INSERT, UPDATE, or DELETE operations are needed when SaveChanges is called.
Tracked entities have states such as Added, Unchanged, Modified, Deleted, and Detached.
Tracking is useful when entities will be modified and saved. For read-only queries, tracking may be unnecessary and AsNoTracking can reduce tracking overhead.
Code Example
var product = await _context.Products
.FirstAsync(p => p.Id == id);
product.Price = 999;
await _context.SaveChangesAsync();