Entity Framework offers a powerful feature known as change tracking.It is a feature that automatically keeps track of modifications made to entities within a DbContext. Based on these tracked changes, EF Core determines which SQL operations—such as INSERT, UPDATE, or DELETE—need to be executed when SaveChanges() is called to persist the updates to the database. EF Core monitors changes at the property level. If only one property is modified, the database update will target just that property. However, a property can be marked as modified only when its parent entity is in the Modified state.
What is Tracking?
In EF Core, tracking queries are the default behavior. When a query is executed, EF Core adds the returned entities to the ChangeTracker. This enables EF Core to:
Monitor changes to these entities
Automatically detect any modifications
Persist updates to the database when SaveChanges() is called
var customer = await context.Customers
.Where(b => b.IsActive)
.ToListAsync(); // Tracking by default
blogs[0].Name = "Updated Name";
await context.SaveChangesAsync(); // Changes are saved
Pros
Cons
What is No-Tracking?
No-tracking queries skip storing entities in the ChangeTracker, making them ideal for read-only operations where updates aren’t required. This approach:
var customer = await context.Customers
.AsNoTracking()
.Where(b => b.IsActive)
.ToListAsync(); // No tracking
blogs[0].Name = "Updated Name";
await context.SaveChangesAsync(); // Will NOT save changes
Pros
Faster queries.
Lower memory footprint.
Cons
Key Differences
| Feature | Tracking Queries | No-Tracking Queries |
|---|
| Change Detection | Yes | No |
| Performance | Slower for large reads | Faster for read-only scenarios |
| Memory Usage | Higher | Lower |
| Suitable For | CRUD operations | Read only Query like Reporting, dashboards |
Conclusion
The choice between Tracking and No-Tracking in EF Core depends on the scenario:
Reference