C# 为上下文的任何实例关闭 EF 更改跟踪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18925111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:37:42  来源:igfitidea点击:

Turn off EF change tracking for any instance of the context

c#entity-frameworkentity-framework-4.1change-tracking

提问by Karl Anderson

I have a context to a read-only database for reporting and I am writing lots of code, like this:

我有一个用于报告的只读数据库的上下文,我正在编写大量代码,如下所示:

using (var context = new ReportingContext())
{
    var reportXQuery = context.ReportX.AsNoTracking();

    // Do stuff here with query...
}

Is there a way to set the AsNoTrackingbit so that just newing up the ReportingContextabove would automatically use AsNoTrackinginstead of needing to remember to explicitly call it every query?

有没有一种方法来设置AsNoTracking一下,让刚new荷兰国际集团了ReportingContext上面会自动使用AsNoTracking,而不是需要记住显式调用它的每一个查询的?

回答by Admir Tuzovi?

Try changing your context constructor to this:

尝试将您的上下文构造函数更改为:

public ReportingContext()
{
this.Configuration.AutoDetectChangesEnabled = false;
}

EDIT:

编辑:

This will after all not help you, as stated on Arthur's blog, it is usable only in particular scenarios:

这毕竟对您没有帮助,正如 Arthur 的博客所述,它仅在特定情况下可用:

http://blog.oneunicorn.com/2012/03/12/secrets-of-detectchanges-part-3-switching-off-automatic-detectchanges/

http://blog.oneunicorn.com/2012/03/12/secrets-of-detectchanges-part-3-switching-off-automatic-detectchanges/