AsNoTracking() 的全局设置? [英] Global setting for AsNoTracking()?
本文介绍了AsNoTracking() 的全局设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
原本我相信
context.Configuration.AutoDetectChangesEnabled = false;
将禁用更改跟踪.但不是.目前我需要在我所有的 LINQ 查询中使用 AsNoTracking()
(对于我的只读层).是否有禁用 DbContext 跟踪的全局设置?
would disable change tracking. But no. Currently I need to use AsNoTracking()
on all my LINQ queries (for my read only layer). Is there a global setting to disable tracking on the DbContext?
推荐答案
由于这个问题没有用特定的 EF 版本标记,我想提一下在 EF Core 中的行为可以是
Since this question is not tagged with a specific EF version, I wanted to mention that in EF Core the behavior can be configured at the context level.
您还可以在上下文中更改默认跟踪行为实例级别:
You can also change the default tracking behavior at the context instance level:
using (var context = new BloggingContext())
{
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var blogs = context.Blogs.ToList();
}
这篇关于AsNoTracking() 的全局设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文