Today we will see the consequences of using AsNoTracking with the Identity Resolution feature available from EF Core 5.0 onwards. I’ve already introduced the Identity Resolution concept in this article, but very briefly, Identity Resolution is used by EF Core to maintain a single instance of a given primary key.
What is AsNoTracking?
The AsNoTracking feature is a suggested option to make the queries faster because, using this feature, the queries do not go through the entity tracking logic. EF Core just goes to the database and brings the data for you.
Every time your application performs a query, EF Core checks if the context is already tracking the required objects. If so, EF Core will return data from the context. If the context does not contain these entities, EF Core will query the database, and the data returned by the database is used to create new instances of the respective entities, and these entities are then appended to the context.
Code example using AsNoTracking:
Note: Based on the project created in the article: Using AsNoTracking
Let’s detail a little more about how entity tracking works and how AsNoTracking works.
Queries performed by EF Core against the database return entity types, and by default, these are tracking queries as tracking is enabled by default in EF Core.
All operations performed on an entity being tracked are maintained by the context, and changes are not saved to the database until the SaveChanges or SaveChangesAsync method is called.
So, every time the application makes a query, EF Core checks if the context is already tracking the required objects, and if the context is tracking, EF Core will return the context’s data. Therefore, data is returned by EF Core without actually querying the database.
If the context does not contain these entities, EF Core will query the database, and the data returned by the database is used to create new instances of the respective entities. These entities are then attached to the context.
No Tracking Means No Identity Resolution
When tracing is enabled, EF Core uses Identity Resolution to keep only a single instance of a given primary key. But when AsNoTracking is used, identity resolution is not used by EF Core.
What does this mean?
Let’s say the results returned by the query contain the primary key repeated multiple times.
If tracing is enabled, a single primary key will only create a single object and attach it to the context.
If tracking is NOT enabled, a new object will be created for each row.
So even if the primary key is the same, EF Core will create multiple entity objects.
Using AsNoTracking with Identity Resolution
However, this changed starting with EF Core 5.0.
Before EF Core 5.0, there was no way to enable identity resolution with AsNoTrakcing.
But now, as of EF Core 5.0, an additional API can be used to enable AsNoTracking and use Identity Resolution.
This code shows the use of the new AsNoTrackingWithIdentityResolution feature.
Here, with AsNoTrackingWithIdentityResolution API, identity resolution is enabled, but no entity tracking is enabled.
Therefore, when a query returns a result set containing multiple rows with the same primary key value, EF Core creates a single object for a given primary key value.
But these objects are not added for tracking.
This option can be useful in some scenarios to reduce the number of objects created (thus optimizing memory usage).
This solves the query problem when we have a one-to-many relationship between entities.
Wait to see what problem this new feature solves in this scenario.
Get the project here: EFCoreIdentityResolution.zip
*The content of this article is the author’s responsibility and does not necessarily reflect the opinion of iMasters.
Leave a comment