如何获取具有多对多关系的相关数据?
How to get related data with many-to-many relationship?
我有模型 Project 和 Programmer 具有多对多关系:
public class Project
{
public Project()
{
ProjectProgrammers = new HashSet<ProjectProgrammer>();
}
public int ProjectID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<ProjectProgrammer> ProjectProgrammers { get; set; }
}
public class Programmer
{
public Programmer()
{
ProjectProgrammers = new HashSet<ProjectProgrammer>();
}
public int ProgrammerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DisplayName { get; set; }
public virtual ICollection<ProjectProgrammer> ProjectProgrammers { get; set; }
}
有模型ProjectProgrammer:
public class ProjectProgrammer
{
public int ProjectID { get; set; }
public virtual Project Project { get; set; }
public int ProgrammerID { get; set; }
public virtual Programmer Programmer { get; set; }
}
配置:
modelBuilder.Entity<ProjectProgrammer>()
.HasKey(pp => new { pp.ProjectID, pp.ProgrammerID });
modelBuilder.Entity<ProjectProgrammer>()
.HasOne(pp => pp.Project)
.WithMany(p => p.ProjectProgrammers)
.HasForeignKey(pp => pp.ProjectID);
modelBuilder.Entity<ProjectProgrammer>()
.HasOne(pp => pp.Programmer)
.WithMany(p => p.ProjectProgrammers)
.HasForeignKey(pp => pp.ProgrammerID);
当我尝试使用 Include() 和 Theninclude() 加载相关数据时:
public async Task<IEnumerable<Project>> ListAsync()
{
return await _context.Projects.Include(p => p.ProjectProgrammers)
.ThenInclude(pp => pp.Programmer)
.ToListAsync();
}
我只得到第一条程序员信息不完整的记录:
[{"projectID":100,"name":"Project1","description":"Desc","projectProgrammers":[{"projectID":100
我通过向其中一个导航 属性 添加 [JsonIgnore] 属性解决了这个问题。更多信息:http://ericsmasal.com/2018/01/04/ef-core-and-defeating-the-self-referencing-loop/
我有模型 Project 和 Programmer 具有多对多关系:
public class Project
{
public Project()
{
ProjectProgrammers = new HashSet<ProjectProgrammer>();
}
public int ProjectID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<ProjectProgrammer> ProjectProgrammers { get; set; }
}
public class Programmer
{
public Programmer()
{
ProjectProgrammers = new HashSet<ProjectProgrammer>();
}
public int ProgrammerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DisplayName { get; set; }
public virtual ICollection<ProjectProgrammer> ProjectProgrammers { get; set; }
}
有模型ProjectProgrammer:
public class ProjectProgrammer
{
public int ProjectID { get; set; }
public virtual Project Project { get; set; }
public int ProgrammerID { get; set; }
public virtual Programmer Programmer { get; set; }
}
配置:
modelBuilder.Entity<ProjectProgrammer>()
.HasKey(pp => new { pp.ProjectID, pp.ProgrammerID });
modelBuilder.Entity<ProjectProgrammer>()
.HasOne(pp => pp.Project)
.WithMany(p => p.ProjectProgrammers)
.HasForeignKey(pp => pp.ProjectID);
modelBuilder.Entity<ProjectProgrammer>()
.HasOne(pp => pp.Programmer)
.WithMany(p => p.ProjectProgrammers)
.HasForeignKey(pp => pp.ProgrammerID);
当我尝试使用 Include() 和 Theninclude() 加载相关数据时:
public async Task<IEnumerable<Project>> ListAsync()
{
return await _context.Projects.Include(p => p.ProjectProgrammers)
.ThenInclude(pp => pp.Programmer)
.ToListAsync();
}
我只得到第一条程序员信息不完整的记录:
[{"projectID":100,"name":"Project1","description":"Desc","projectProgrammers":[{"projectID":100
我通过向其中一个导航 属性 添加 [JsonIgnore] 属性解决了这个问题。更多信息:http://ericsmasal.com/2018/01/04/ef-core-and-defeating-the-self-referencing-loop/