AsNoTracking on context properties, query or ChangeTracker?

久未见 提交于 2019-12-05 05:51:27

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.

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!