Entity Framework  

Tracking vs No-Tracking in Entity Framework Core

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

  • Automatic change detection.

  • Simplifies update logic.

Cons

  • Higher memory and CPU overhead.

  • Slower for large queries.

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:

  • Boosts performance by eliminating tracking overhead

  • Minimizes memory usage for large result sets

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

  • Cannot persist changes without manual attach.

Key Differences

FeatureTracking QueriesNo-Tracking Queries
Change DetectionYesNo
PerformanceSlower for large readsFaster for read-only scenarios
Memory UsageHigherLower
Suitable ForCRUD operationsRead only Query like Reporting, dashboards

Conclusion

The choice between Tracking and No-Tracking in EF Core depends on the scenario:

  • Use Tracking for CRUD operations where entity state changes need to be monitored.

  • Use No-Tracking for read-only queries to improve performance.

Reference