EF Core Change Tracking: Transitioning from Default to Global NoTracking
27K reputation · 31 Dec 2022, 21:29 UTC
In high-read applications using Entity Framework Core, the default tracking behavior can introduce significant memory overhead due to the identity map and snapshotting processes. While AsNoTracking() provides granular control per query, configuring QueryTrackingBehavior.NoTracking globally in the DbContext options shifts the responsibility of state management entirely to the developer.
This transition affects the standard Unit of Work pattern. When global no-tracking is enabled, entities retrieved via LINQ queries are not monitored by the Change Tracker, meaning subsequent modifications will not be persisted during SaveChanges() unless the entities are manually attached or their states are explicitly updated.
- How does the behavior of
DbSet.Find()differ from LINQ queries when global no-tracking is configured? - What is the most efficient method to re-attach a specific subset of non-tracked entities for updates without triggering unnecessary database round-trips?