我有一些像这样的复杂查询:
var contents = await db.Contents
.Where(q => q.ContentStatusId == contentStatusId || contentStatusId == 0)
.Where(q => q.ContentTypeId == contentTypeId || contentTypeId == 0)
.Where(q => q.CreatedBy == contentCreatedBy || contentCreatedBy == "0")
.Where(q => q.ModifiedBy == contentModifiedBy || contentModifiedBy == "0")
.Where(q => q.SubjectId == subjectId)
.ToListAsync();
我将结果发送回我的网络API,我不需要任何跟踪。 EF是否会自动添加跟踪,如果是,那就是我不需要的开销。如果确实如此,那么我可以关闭跟踪吗?
答案 0 :(得分:1)
是的,有一个开销。您可以在IQueryable或DbSet上使用AsNoTracking()。
var contents = await db.Contents
.Where(q => q.ContentStatusId == contentStatusId || contentStatusId == 0)
.Where(q => q.ContentTypeId == contentTypeId || contentTypeId == 0)
.Where(q => q.CreatedBy == contentCreatedBy || contentCreatedBy == "0")
.Where(q => q.ModifiedBy == contentModifiedBy || contentModifiedBy == "0")
.Where(q => q.SubjectId == subjectId)
.AsNoTracking()
.ToListAsync();