AsNoTracking()和包含 [英] AsNoTracking() and Include
本文介绍了AsNoTracking()和包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Linq查询,可获取一个实体及其某些导航属性.
I have a Linq query that fetches an entity and some of its navigation properties.
context.MyEntity
.AsNoTracking()
.Include(i=> i.Nav1)
.Include(i=> i.Nav2)
.Where(x=> x.Prop1==1)
.FirstOrDefault();
我的问题是:
上述查询是否足以跟踪MyEntity
或导航属性NAv1
& Nav2
,还是我必须为每个导航属性添加AsNoTracking
?
Is the above query enough to not track MyEntity
nor the navigation properties NAv1
& Nav2
or must I add AsNoTracking
for each navigation property?
像这样:
context.MyEntity
.AsNoTracking()
.Include(i=> i.Nav1)
.AsNoTracking()
.Include(i=> i.Nav2)
.AsNoTracking()
.Where(x=> x.Prop1==1)
.FirstOrDefault();
推荐答案
在完成所有查询参数之后,但在将数据移入内存之前,请使用AsNoTracking.在此示例中,您需要:
Use AsNoTracking after you have completed all your query parameters but before you move the data into memory. In this example, you'll want:
context.MyEntity
.Include(i=> i.Nav1)
.Include(i=> i.Nav2)
.Where(x=> x.Prop1==1)
.AsNoTracking()
.FirstOrDefault();
父实体的任何子对象将不会被跟踪.
Any child objects of the parent entity will not be tracked.
这篇关于AsNoTracking()和包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文