I noticed that it is possible to disable tracking in three different ways:
AsNoTracking
on context propertiesAsNoTracking
on the final query before executing itcontext.ChangeTracker.QueryTrackingBehavior
Is there any difference between these three approaches if I want to disable tracking for everything?
If I previously used AsNoTracking
after each context property and now I replace it with only a single call on the final query (or disable it via the ChangeTracker
) will it have the same effect?
AsNoTracking
and AsTracking
are extension methods of IQueryable<T>
, thus are associated with the state of the query and not a specific entity (the fact that they are available at DbSet<T>
level is just because it implements IQueryable<T>
) - note the word all inside the method descriptions:
AsNoTracking
Returns a new query where the change tracker will not track any of the entities that are returned.
AsTracking
Returns a new query where the change tracker will keep track of changes for all entities that are returned.
And both say:
The default tracking behavior for queries can be controlled by QueryTrackingBehavior.
In other words, if the query returns entities and there is no AsNoTracking
or AsTracking
calls anywhere in the query expression tree, the query uses the value of the ChangeTracker.QueryTrackingBehavior
.
So the answer to your question is yes, you can achieve the same effect with a single call on the final query or via ChangeTracker
.
There is one thing to note though, which is not explained in the documentation. If the query expression tree contains more than one AsNoTracking
/ AsTracking
calls, the last call takes precedence. Which means that by adding AsNoTracking
or if you add AsTracking
to the final query will control it's behavior regardless of the any inner tracking behavior calls or ChangeTracker
property.
AsNoTracking
works for individual query level
using (var context = new YourContext())
{
var data = context.Entity
.AsNoTracking()
.ToList();
}
You can also change the default tracking behavior at the context instance level:
If you want to do this at context instance level, you can do like this, unlike EF 6 (AsNoTracking() is mandatory each time you make query):
using (var context = new YourContext())
{
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var data = context.Entity.ToList();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With