.NET EF Core - No Tracking and Identity Resolution
.NET EF Core - No Tracking and Identity Resolution

AsNoTracking with Identity Resolution In .NET EF Core

We have seen what is identity resolution and how it is used by EF core to track the entities in memory.

AsNoTracking is very common option suggested to make querying faster. It is faster because queries do not go via entity tracking logic. It just goes to database and brings the data for you. But in addition, an important feature Identity Resolution are not available to entities fetched using AsNoTracking when it is used.

Let’s have a look at how they are related.

How does tracking works ?

EF Core can query the data from database. These queries return entity types. By default, such queries are tracking queries. So, if application wants EF core to track some entities, nothing needs to be specified in the query.

All operations performed on an entity which is being tracked, are hold by the context. The changes do not get saved to database until SaveChanges or SaveChangesAsync method is called.

Every time, when application queries something, EF core checks if context already is tracking the required objects. If context has them, then EF core returns data from the context. So, the data is returned by EF core without actually querying to the database.

If the context does not contain those entities, then EF core makes query to the database. The data returned by database is used to create new instances of the respective entities. These entities are then attached to the context.

What is “No Tracking” ?

No Tracking” means entities would not be tracked by context. This option is generally used when the entities fetched from database are not required to be updated back to database.

For enabling “No Tracking“, we need to add “AsNoTracking” option to the queries. When this option is specified, the queries are directly sent to database

Note that when AsNoTracking is used, the query would go to database for fetching records, even if context already is tracking all or some of the entities asked by “AsNoTracking” query.

AsNoTracking is generally a bit faster than tracking as there is no intermediate layer for tracking in between application (i.e. repository) and database.

No Tracking means No Identity Resolution

This section title is exactly opposite of the blog post title. But please bear with me for next few moments.

When tracking is enabled, EF core uses identity resolution to maintain 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 contains primary key repeated multiple times.

  • If tracking is enabled, a single primary key would create only one single object and will attach it to context.
  • If tracking is NOT enabled, then for every row, a new object would be created. So even though primary key is same, EF core will create multiple objects of the entity.

Below is a sample code which shows how AsNoTracking can be used in the query.

No Tracking With Identity Resolution

In previous section, we have seen that –

When tracking is enabled, EF core uses identity resolution to maintain only a single instance of a given primary key. But when AsNoTracking is used, identity resolution is not used by EF Core.

This is confusing. Previous section says identity resolution is not used by EF Core and now this section is saying identity resolution can be used.

Well, previous section was talking about the past versions of EF Core. Before EF Core 5.0, there was no way to enable identity resolution with “no tracking”. But now, with EF Core 5.0, there is an additional API that can be used to enable both “No Tracking” and identity resolution.

Here, with AsNoTrackingWithIdentityResolution API, identity resolution is enabled, but it does not enable tracking of entities.

So, when a query returns a resultset, containing multiple rows having same primary key value, EF Core would create a single object for a given primary key value. But these objects are not added for tracking. This option may be helpful in some scenarios to reduce number of objects created (thereby optimizing memory usage).

I hope you find this information helpful. Let me know your thoughts.

Leave a ReplyCancel reply