Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of AsNoTrackingWithIdentityResolution() and when to use it?

I get why we use AsNoTracking() to increase performance on queries to aovid the overhead of tracking in entity framework, but I find kind of confusing the purpose of AsNoTrackingWithIdentityResolution().

Can someone explain it to me like if I were 5?

like image 737
José Alvarez Avatar asked Oct 20 '25 12:10

José Alvarez


1 Answers

It's been a long time this question unanswered because it's important to mention that AsNoTrackingWithIdentityResolution has the worst performance!

In a very simple way, by using AsNoTracking you tell EF that don't track any entity, right?

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 means identity resolution is not used by EF, so we can say No tracking and No identity resolution!

By using AsNoTrackingWithIdentityResolution, identity resolution is enabled, but it does not enable tracking of entities. So if there is multiple objects with the same primary key, EF will reuse only one object for them.

But there is an open issue on EF core repo that shows AsNoTrackingWithIdentityResolution has some problem with its implementation and caused bad performance, follow the issue here: https://github.com/dotnet/efcore/issues/23558

like image 181
Saeed Esmaeelinejad Avatar answered Oct 23 '25 08:10

Saeed Esmaeelinejad