首页 > 解决方案 > 如何加入三个table rest api .net core 5?

问题描述

我有三个实体。

第一个 x 第二个 y 第三个 z

    public int Id { get; set; }
    public ICollection<y> y{ get; set; }
    public virtual z z{ get; set; }

          For Y

    public int Id { get; set; }
    public int xID{ get; set; }
    public x x { get; set; }

   FOR Z

    public int Id { get; set; }
    public int xId { get; set; }
    public x x { get; set; }

当我尝试返回 await _context.x.ToListAsync(); y 和 z 列为空。这是为什么?如何相互连接?

我使用流利的 api 来建立关系。

其实我需要这个查询。如何写异步?

select table1.ID ,table1.Name from Table1 inner join Table2 on Table1 .ID =Table2 .ID
inner join Table3 on table2.ID=Table3 .ID

标签: c#.netapirest

解决方案


你错过了一些Include()电话。

您的查询应该是

 await _context.x.Include(e => e.y).Include(e => e.z).ToListAsync();

推荐阅读