Turn off EF change tracking for any instance of the context [duplicate]

北慕城南 提交于 2019-12-28 06:28:16

问题


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 newing up the ReportingContext above would automatically use AsNoTracking instead of needing to remember to explicitly call it every query?


回答1:


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:

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



来源:https://stackoverflow.com/questions/18925111/turn-off-ef-change-tracking-for-any-instance-of-the-context

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