使用AsNoTracking()获取实体是否禁用对DetectChanges()的自动调用? [英] Does getting entities with AsNoTracking() disable the automatic call to DetectChanges()?

查看:339
本文介绍了使用AsNoTracking()获取实体是否禁用对DetectChanges()的自动调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经认识到这个 AsNoTracking() DetectChanges() AutoDetectChangesEnabled 最近。据了解,当使用 AsNoTracking()的实体框架从数据库中获取记录时,实体框架不会跟踪这些记录上的任何更改,并更新所提取记录的任何属性在这种情况下,将会失败。



我的问题是如果以这种方式提取记录,是否会禁用对DetectChanges()的自动调用,否则必须通过设置:

  Context.Configuration.AutoDetectChangesEnabled = false; 

还要告诉我,如果这两个操作都有什么影响(在性能方面)在严格用于仅读目的的情况下执行数据时执行:

  Context.Configuration.AutoDetectChangesEnabled = false; 
Context.Set&T;()。AsNoTracking();


解决方案


禁用自动调用DetectChanges()


不,不会。但是您必须意识到,除了作为EF的一部分, AsNoTracking DetectChanges 没有任何关系。无论AutoDetectChanges是否启用,使用 AsNoTracking 获取的对象将永远不会被检测到。此外, AsNoTracking DbSet 级别, AutoDetectChangesEnabled 上下文级别。有一个 DbSet 方法会影响整个上下文是不好的。


或[设置 AutoDetectChangesEnabled ]必须明确完成


嗯,你可能不应该禁用AutoDetectChanges。 如果两个操作都执行了


如上所述,它们不相关。他们都可以以自己的方式提高绩效。




  • 如果要提取只读数据,AsNoTracking 非常棒。它没有副作用(如:其效果很清楚)

  • 设置 AutoDetectChangesEnabled = false 停止自动调用 DetectChanges (可能很多),但它有副作用,您需要注意。来自Lerman& Miller的书 DbContext :


    需要调用DetectChanges时工作不像
    可能会出现。实体框架小组强烈建议您如果遇到
    性能问题,则只能将
    交换为手动调用DetectChanges。还建议只有选择不使用自动
    DetectChanges来执行性能差的部分代码,并且在有关部分执行完毕后重新启用




I've come to know this concept of AsNoTracking(), DetectChanges(), and AutoDetectChangesEnabled very recently. I understand that when fetching records from the database via Entity Framework with AsNoTracking() used, then Entity Framework does not track any changes on those records and updating any property of the fetched record will fail in that case.

My question is if records are fetched in that manner, will it also cause disabling the automatic call to DetectChanges() or does that have to be done explicitly by setting:

Context.Configuration.AutoDetectChangesEnabled = false;

Also kindly let me know what impact (in terms of performance) does it have if both of the actions are performed while fetching the data strictly for read only purposes:

Context.Configuration.AutoDetectChangesEnabled = false;
Context.Set<T>().AsNoTracking();

解决方案

will it also cause disabling the automatic call to DetectChanges()

No it won't. But you must realize that AsNoTracking and DetectChanges have nothing to do with each other (apart from being part of EF). Objects fetched with AsNoTracking will never be change detected anyway, whether AutoDetectChanges is enabled or not. Besides, AsNoTracking works on a DbSet level, AutoDetectChangesEnabled on the context level. It would be bad to have a DbSet method affect the whole context.

or that [setting AutoDetectChangesEnabled] has to be done explicitly

Well, you probably just shouldn't disable AutoDetectChanges. If you do it you must know what you do.

what impact(in terms of performance) does it have if both of the action is performed

As said, they are not related. They can both improve performance in their own way.

  • AsNoTracking is great if you want to fetch read-only data. It has no side effects (as in: its effect is clear)
  • Setting AutoDetectChangesEnabled = false stops automatic calls of DetectChanges (which can be numerous), but it has side effects you need to be aware of. From Lerman & Miller's book DbContext:

    Working out when DetectChanges needs to be called isn’t as trivial as it may appear. The Entity Framework team strongly recommends that you only swap to manually calling DetectChanges if you are experiencing performance issues. It’s also recommended to only opt out of automatic DetectChanges for poorly performing sections of code and to reenable it once the section in question has finished executing.

这篇关于使用AsNoTracking()获取实体是否禁用对DetectChanges()的自动调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆