HttpGet 与三个关系表与 dot net 核心 web api
HttpGet with three relational tables with dot net core web api
我有树状表 Sectors 、 Centers 和 Managements
部门和中心之间的关系是一对多的,中心和管理之间的关系也是一对多的
我想检索 Json 以获取部门有中心列表和中心有管理列表
但我得到了这个结果
[{"secID":4,"cod":"400000000","name":"قطاع الأمن والخدمات المالية والإدارية","Centers":[{"ID":18,"Cent":1.0,"name":"الإدارة العامة لمكتب رئيس القطاع\"5","Mangments":[],"cod":"0401000000","SecID":4},{"ID":19,"Cent":2.0,"name":"(أ) الإدارة المركزية للخدمات الأمنية","Mangments":[],"cod":"0402000000","SecID":4},{"ID":20,"Cent":3.0,"name":"(ب) الإدارة المركزية للخدمات الإدارية","Mangments":[],"cod":"0403000000","SecID":4},{"ID":21,"Cent":4.0,"name":"(ج) الإدارة المركزية للخدمات المالية","Mangments":[],"cod":"0404000000","SecID":4}]}
管理:[]
这是我的代码
public class Sectors
{
[Key]
public Int32 secID { get; set; }
public string cod { get; set; }
public string name { get; set; }
public virtual ICollection<Centers> Centers { get; set; }
= new List<Centers>();
}
public class Centers
{
[Key]
public Int32 ID { get; set; }
public double? Cent { get; set; }
public string name { get; set; }
public virtual ICollection<Mangments> Mangments { get; set; }
= new List<Mangments>();
public string cod { get; set; }
[ForeignKey("SecID")]
//public Sectors sectors { get; set; }
public Int32 SecID { get; set; }
}
public class Mangments
{
[Key]
public Int32 ID { get; set; }
public string cod { get; set; }
public string name { get; set; }
public double? mang { get; set; }
[ForeignKey("CenterID")]
//public Sectors sectors { get; set; }
public Int32 CenterID { get; set; }
}
这是获取方法的代码
[HttpGet]
public async Task<ActionResult<IEnumerable<Sectors>>> GetSectors()
{
return _context.Sectors.OrderBy(c => c.name).Include(c => c.Centers).ToList();
}
我可以更新什么以获得我想要的结果
您可以使用 Select() 方法:
_context.Sectors.OrderBy(c => c.name)
.Include(s => s.Centers.Select(c => c.Mangments))
.ToList();
或
您可以使用 ThenInclude() 方法
https://entityframeworkcore.com/querying-data-include-theninclude
_context.Sectors.OrderBy(c => c.name)
.Include(c => c.Centers)
.ThenInclude(c => c.Mangments)
.ToList();
我有树状表 Sectors 、 Centers 和 Managements 部门和中心之间的关系是一对多的,中心和管理之间的关系也是一对多的 我想检索 Json 以获取部门有中心列表和中心有管理列表 但我得到了这个结果
[{"secID":4,"cod":"400000000","name":"قطاع الأمن والخدمات المالية والإدارية","Centers":[{"ID":18,"Cent":1.0,"name":"الإدارة العامة لمكتب رئيس القطاع\"5","Mangments":[],"cod":"0401000000","SecID":4},{"ID":19,"Cent":2.0,"name":"(أ) الإدارة المركزية للخدمات الأمنية","Mangments":[],"cod":"0402000000","SecID":4},{"ID":20,"Cent":3.0,"name":"(ب) الإدارة المركزية للخدمات الإدارية","Mangments":[],"cod":"0403000000","SecID":4},{"ID":21,"Cent":4.0,"name":"(ج) الإدارة المركزية للخدمات المالية","Mangments":[],"cod":"0404000000","SecID":4}]}
管理:[] 这是我的代码
public class Sectors
{
[Key]
public Int32 secID { get; set; }
public string cod { get; set; }
public string name { get; set; }
public virtual ICollection<Centers> Centers { get; set; }
= new List<Centers>();
}
public class Centers
{
[Key]
public Int32 ID { get; set; }
public double? Cent { get; set; }
public string name { get; set; }
public virtual ICollection<Mangments> Mangments { get; set; }
= new List<Mangments>();
public string cod { get; set; }
[ForeignKey("SecID")]
//public Sectors sectors { get; set; }
public Int32 SecID { get; set; }
}
public class Mangments
{
[Key]
public Int32 ID { get; set; }
public string cod { get; set; }
public string name { get; set; }
public double? mang { get; set; }
[ForeignKey("CenterID")]
//public Sectors sectors { get; set; }
public Int32 CenterID { get; set; }
}
这是获取方法的代码
[HttpGet]
public async Task<ActionResult<IEnumerable<Sectors>>> GetSectors()
{
return _context.Sectors.OrderBy(c => c.name).Include(c => c.Centers).ToList();
}
我可以更新什么以获得我想要的结果
您可以使用 Select() 方法:
_context.Sectors.OrderBy(c => c.name)
.Include(s => s.Centers.Select(c => c.Mangments))
.ToList();
或
您可以使用 ThenInclude() 方法 https://entityframeworkcore.com/querying-data-include-theninclude
_context.Sectors.OrderBy(c => c.name)
.Include(c => c.Centers)
.ThenInclude(c => c.Mangments)
.ToList();