AsNoTracking on context properties, query or ChangeTracker?

c# entity-framework-core

Question

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?

1
7
9/19/2018 7:59:44 AM

Accepted Answer

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 AsNoTracking to the final query will control it's behavior regardless of the any inner tracking behavior calls or ChangeTracker property.

4
9/21/2018 6:59:02 AM

Popular Answer

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();
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow