linq语句中的AsNoTracking方法抛出错误

时间:2019-03-04 18:11:28

标签: c# oracle entity-framework linq

以下是我从数据库中获取信息的查询。从性能角度来看,我想在实体上使用AsNoTracking方法。

var teamrole = (from role in _context.USERROLEs.AsNoTracking()
    from team in _context.TEAMROLEs.AsNoTracking().Where(x => x.ID == role.ROLEID).DefaultIfEmpty()
    from queue in _context.WORKQUEUEs.AsNoTracking().Where(x => x.ID == team.QUEID).DefaultIfEmpty()
    where team.ROLECD == roleCode
    select new { team, queue }).ToList();

这是我的上下文:

public partial class MyContext : DbContext
{
    public MyContext() : base("MyContext")
    {
        Configuration.ProxyCreationEnabled = false;
        Database.SetInitializer<MyContext>(null);
    }

    public virtual DbSet<ZTEAMROLE> ZTEAMROLEs { get; set; }    
    //Some other entities here...
}

实体:

[Table("TEAMROLE")]
public partial class TEAMROLE
{
    [StringLength(36)]
    public string ID { get; set; }  

    [StringLength(36)]
    public string QUEID { get; set; }

    //More Properties...
}

我在执行查询teamrole时遇到的错误

  

方法   System.Data.Entity.Infrastructure.DbQuery'1[XXX.Entities.TEAMROLE] AsNoTracking()在类型上声明   System.Data.Entity.Infrastructure.DbQuery'1[XXX.Entities.TEAMROLE]   不能使用类型的实例调用   System.Data.Entity.Core.Objects.ObjectQuery'1[XXX.Entities.TEAMROLE]

1 个答案:

答案 0 :(得分:2)

您只需在查询中调用一次.AsNoTracking,而不必在每个DbSet<T>上调用:

var teamrole = (from role in _context.USERROLEs
    from team in _context.TEAMROLEs.Where(x => x.ID == role.ROLEID).DefaultIfEmpty()
    from queue in _context.WORKQUEUEs.Where(x => x.ID == team.QUEID).DefaultIfEmpty()
    where team.ROLECD == roleCode
    select new { team, queue })
    .AsNoTracking()
    .ToList();