How does EF Core's change tracking work, and what is the difference between tracked and no-tracking queries?
Short answer
By default, EF Core's DbContext tracks every entity returned by a query, recording a snapshot of its original values so SaveChanges can detect and generate SQL for exactly what changed.
Advertisement
AModel answer
By default, EF Core's DbContext tracks every entity returned by a query, recording a snapshot of its original values so SaveChanges can detect and generate SQL for exactly what changed. AsNoTracking() disables this tracking for a query, improving performance and reducing memory usage for read-only scenarios (like displaying data with no intent to update it), since the context doesn't need to maintain change-detection snapshots for entities that will never be modified and saved back.
// Tracked (default): needed if you plan to modify and SaveChanges
var product = await context.Products.FirstAsync(p => p.Id == 1);
product.Price = 19.99m;
await context.SaveChangesAsync();
// No-tracking: faster for read-only display scenarios
var products = await context.Products.AsNoTracking().ToListAsync();Real-world example
A reporting dashboard querying thousands of records for read-only display uses AsNoTracking() throughout, measurably reducing memory usage and query time compared to the default tracked behavior which was unnecessary for a page that never saves changes back.
Likely follow-ups
- How much memory/performance overhead does tracking actually add per entity?
- When would AsNoTrackingWithIdentityResolution be preferable to plain AsNoTracking?
Related topics