gpt4 book ai didi

c# - AsNoTracking 方法在 linq 语句中抛出错误

转载 作者:行者123 更新时间:2023-12-04 16:29:10 24 4
gpt4 key购买 nike

下面是我从数据库中获取信息的查询。从性能的角度来看,我想使用 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

Method System.Data.Entity.Infrastructure.DbQuery'1[XXX.Entities.TEAMROLE] AsNoTracking() declared on type System.Data.Entity.Infrastructure.DbQuery'1[XXX.Entities.TEAMROLE] cannot be called with instance of type System.Data.Entity.Core.Objects.ObjectQuery'1[XXX.Entities.TEAMROLE]

最佳答案

您只需调用.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();

关于c# - AsNoTracking 方法在 linq 语句中抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54989151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com