Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsNoTracking on context properties, query or ChangeTracker?

I noticed that it is possible to disable tracking in three different ways:

  • via AsNoTracking on context properties
  • via AsNoTracking on the final query before executing it
  • via context.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?

like image 796
t3chb0t Avatar asked Sep 19 '18 07:09

t3chb0t


2 Answers

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.

like image 157
Ivan Stoev Avatar answered Sep 24 '22 07:09

Ivan Stoev


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();
}
like image 27
vivek nuna Avatar answered Sep 24 '22 07:09

vivek nuna