gpt4 book ai didi

sql-server - 多个 Include/ThenInclude 在 EF Core 中导致重复

转载 作者:行者123 更新时间:2023-12-05 06:39:22 25 4
gpt4 key购买 nike

好的,所以我有一个表 Building,其中包含该建筑物中的所有人员。然而,每个人都有一个职业,它本身就是一个实体,根据可用的内容添加到 Person 实体。

var data = _dbcontext.Building
.Where(m => m.BuildingId == buildingId)
.Include(x => x.Person).ThenInclude(x => x.Doctor)
.Include(x => x.Person).ThenInclude(x => x.Teacher)
.Include(x => x.Person).ThenInclude(x => x.Farmer)
.Include(x => x.Person).ThenInclude(x => x.Prostitute);

这是我在网上找到的方法,但它似乎真的包含了 Person 实体 4 次,所以我有很多重复项。我真的只想要一次,但我想尽可能离开加入职业实体。

外键都已正确设置,我使用 EF 来创建我的模型。这是 Person 实体的样子:

public partial class Person {
public int PersonId { get; set; }
public int? DoctorId { get; set; }
public int? TeacherId { get; set; }
public int? FarmerId { get; set; }
public int? ProstituteId { get; set; }
public int BuildingId { get; set; }

public Doctor Doctor { get; set; }
public Teacher Teacher { get; set; }
public Farmer Farmer { get; set; }
public Prostitute Prostitute { get; set; }
}

最佳答案

var data = _dbcontext.Person
.Where(p => p.BuildingId == buildingId)
.Include(p => p.Building)
.Include(p => p.Doctor)
.Include(p => p.Teacher)
.Include(p => p.Farmer)
.Include(p => p.Prostitute)
.OrderBy(p => p.Building;

关于sql-server - 多个 Include/ThenInclude 在 EF Core 中导致重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44645080/

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