AsNoTracking on context properties, query or ChangeTracker?

社会主义新天地 提交于 2019-12-10 03:56:32

问题


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:


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.




回答2:


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


来源:https://stackoverflow.com/questions/52400940/asnotracking-on-context-properties-query-or-changetracker

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