我很难为 dto 构建树层次结构。树层次结构被捕获在具有深度的闭包表中。闭包表是使用数据库中的触发器构建的,并且没有关系映射到 EF 中的 Teams 表。
这是我的基本类结构:
public class Team
{
public Guid Id {get; set;}
public string Title {get; set;}
public Guid? ParentTeamId {get; set;}
public Team ParentTeam {get; set;}
}
public class TeamDto
{
public Guid Id {get; set;}
public string Title {get; set;}
public Guid? ParentTeamId {get; set;}
public ICollection<TeamDto> SubTeams {get; set;} = new List<TeamDto>();
}
闭包表的结构如下:
public class Closure
{
public Guid ParentId {get; set;}
public Guid ChildId {get; set;}
public int Depth {get; set;}
}
EF 定义如下:
public void Configure(EntityTypeBuilder<Team> builder)
{
builder.HasOne(p => p.ParentTeam)
.WithMany()
.HasForeignKey(k => k.ParentTeamId)
.IsRequired(false)
.OnDelete(DeleteBehavior.Cascade);
}
public void Configure(EntityTypeBuilder<Closure> builder)
{
builder.HasKey(aa => new {aa.ChildId, aa.ParentId});
}
对此进行查询时,我能做的最好的事情就是在第一级填充 subTeams。我正在使用 AutoMapper,但不喜欢 AutoMapper 解决方案。
我获取与用户相关的团队列表的查询(已经在 teamMemberships 列表中捕获,_mapper 是 AutoMapper 注入,_context 是我的数据库上下文注入)如下所示:
var teamList = await _context.Closures
.Where(x => teamMemberships.Contains(x.ChildId))
.Select(x => x.ParentId)
.ToListAsync();
var query = _context.Teams
.Where(d => teamList.Contains(d.Id) && x.ParentTeamId == null)
.Select(x => new TeamDto
{
Id = x.Id,
ParentTeamId = x.ParentTeamId,
Title = x.Title,
SubTeams = _mapper.Map<List<TeamDto>>(_context.Teams.Where(y => y.ParentTeamId == x.Id))
})
.AsQueryable();
我需要以某种方式递归地拉出 SubTeams,以便在 Dto 中建立完整的树。我在想可能有一个引用闭包表的查询可以做到这一点。以 json 格式发送的 dto 应如下所示:
[
{
"id": "e5500e84-21fb-4b5e-aa9b-a95e51d3dd05",
"parentId": null,
"title": "Team 1",
"subTeams": [
{
"id": "05a8d423-feb1-4ea6-9596-d26b18feaa9e",
"parentId": "e5500e84-21fb-4b5e-aa9b-a95e51d3dd05",
"title": "Team 1.1",
"subTeams": [
{
"id": "251358d9-3c5a-43b5-91f1-c2e82778c50b",
"parentId": "05a8d423-feb1-4ea6-9596-d26b18feaa9e",
"title": "Team 1.1.1",
"subTeams": []
},
{
"id": "4e27be0b-3a7b-4c8c-8039-2a18e1afcbd0",
"parentId": "05a8d423-feb1-4ea6-9596-d26b18feaa9e",
"title": "Team 1.1.2",
"subTeams": []
}
]
},
{
"id": "2a4d56fc-00f0-4418-80a2-7d870c0ddee5",
"parentId": "e5500e84-21fb-4b5e-aa9b-a95e51d3dd05",
"title": "Team 1.2",
"subTeams": []
}
]
},
...
]
非常感谢任何见解。