使用EF6.1时是否存在跟踪开销?如果是,我该如何删除它?

时间:2014-06-27 13:46:29

标签: asp.net asp.net-mvc entity-framework

我有一些像这样的复杂查询:

  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是否会自动添加跟踪,如果是,那就是我不需要的开销。如果确实如此,那么我可以关闭跟踪吗?

1 个答案:

答案 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();