Pro EP 25 : Tracking vs Non Tracking vs Identity Resolution in Entity Framework

Muhammad Waseem
Become .NET Pro !
2 min readMar 18, 2023

𝐓𝐫𝐚𝐜𝐤𝐢𝐧𝐠 : By default all queries are tracked , so behind the seen a change tracker keeps working on each entity. If you want to perform Update/Delete then you should use tracking.

𝐍𝐨𝐧 𝐓𝐫𝐚𝐜𝐤𝐢𝐧𝐠 : If you just need read only data then you don’t need your queries to keep tracking of change , you can simply go with no tracking and help the query run more speedy.

𝐈𝐝𝐞𝐧𝐭𝐢𝐭𝐲 𝐑𝐞𝐬𝐨𝐥𝐮𝐭𝐢𝐨𝐧 : When you use a tracking query in Entity Framework Core, it uses identity resolution to return the same entity instance from the change tracker if it’s already being tracked

However, a no-tracking query doesn’t use the change tracker and always returns a new instance of the entity, even if the same entity is contained in the result multiple times.

To combine both behaviors, EF Core added AsNoTrackingWithIdentityResolution(), which uses a stand-alone change tracker to do identity resolution without tracking the results in the context.

Once the query is enumerated, the change tracker is garbage collected.

We can add NoTracking at database context level as well

So in one picture !

If you want to help the author in growing

  1. Subscribe my Weekly .NET Newsletter of C#/.NET with 700+ Software Engineers.
  2. Become Patron to get access to 90+ Compiled Questions and Answers at one place.
  3. Download my eBook at Gum road that contains 30+ .NET Tips.

--

--