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
Turn off EF change tracking for any instance of the context
提问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 AsNoTracking
bit so that just new
ing up the ReportingContext
above would automatically use AsNoTracking
instead 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 的博客所述,它仅在特定情况下可用: