What is AsNoTracking, and when should you use it?
Quick Answer
AsNoTracking() tells EF Core not to add the returned entities to the change tracker. The query is faster and uses less memory because EF skips building change-tracking snapshots. Use it for read-only queries — lists, reports, API GET responses — where you won't call SaveChanges() on the results.
AsNoTracking stops EF from registering the results in the change tracker. Normally EF keeps a snapshot of every entity it loads so it can work out what changed on SaveChanges — but if I'm just reading data for a list or an API response, I'm never going to save it, so that snapshot work is wasted. Turning it off makes the query noticeably faster and lighter on large result sets. If the same entity appears more than once in the results and I need it to be the same object, there's AsNoTrackingWithIdentityResolution. I often set no-tracking as the context default and opt back in with AsTracking for the write paths.
- Tracking (the default) builds a snapshot of every entity so
SaveChangescan diff it. For a read you never save, that's pure overhead. AsNoTrackingcan be 20–30%+ faster on large result sets and cuts memory.- Identity resolution: plain
AsNoTrackingreturns a fresh object per row, so the same entity referenced twice becomes two objects.AsNoTrackingWithIdentityResolution()keeps them deduplicated without full tracking. - Set it as the default for the whole context with
UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)and opt back into tracking withAsTracking()where you need it. - Projections into a DTO (
Selectto a non-entity type) are already not tracked.