簡體   English   中英

如何使用 Entity Framework Core 在 .NET Core WebAPI 中返回嵌套表(一對多)?

[英]How to return nested tables (one-to-many) in .NET Core WebAPI using Entity Framework Core?

我是 .NET 的新手,無法弄清楚為什么我會得到某些結果。 我發現了一些關於急切加載與延遲加載的結果,但沒有一個解決方案有效。

我已經更改了列和數據的名稱,因為它與工作相關,所以沒有公共 repo 可用。

目前我的前端正在接收這樣的數據:

{
    id: 234,
    column1: 1,
    column2: 2,
    column3: null
}

我希望它像這樣在嵌套數組中返回一對多關系:

{
    id: 234,
    column1: 1,
    column2: 2,
    column3: [
        {
            table2Column1: 1,
            table2Column2: 2,
            table2Column3: 3,
        },
        {
            table2Column1: 7,
            table2Column2: 8,
            table2Column3: 9,
        },
    ]
}

以下是我的模型和 Controller 模式:

// Table1.cs

...
public int Id { get; set; }
public int Column1 { get; set; }
public int Column2 { get; set; }
public ICollection<Table2> Column3 { get; set; }
...

// Table2.cs
...
public int Id { get; set; }
public int Table2Column1 { get; set; }
public int Table2Column2 { get; set; }
public int Table2Column3 { get; set; }

public int Table1Id { get; set; }
public Table1 Table1 { get; set; }
...

//Table1Controller.cs
...
// GET: api/table1/{id}
[HttpGet("{id}")]
public Task<ActionResult<Table1>> GeTable1ById(int id)
{
    return await _context.Table1.FindAsync(id);
}
...

我發現信息說必須使用 Include 語句來強制預加載,所以我將 controller 更改為:

//Table1Controller.cs
...
// GET: api/table1/{id}
[HttpGet("{id}")]
public ActionResult<Table1> GeTable1ById(int id)
{
    return _context.Table1
                      .Include("Table2")
                      .Where(p => p.Id == id);
}
...

我嘗試了一些變體,但錯誤的要點是我改變了數據的形狀,所以它不適合 model... 盡管它應該是因為它應該預期來自鏈接表的 ICollection。 EFCore 在創建遷移和更新數據時沒有問題,因為使用外鍵的 SQLServer 中的一切都是正確的。

返回的錯誤是: error CS0029: Cannot implicitly convert the type 'System.Linq.IQueryable<Project.Models.Table1>' to 'Microsoft.AspNetCore.Mvc.ActionResult<Project.Models.Table1>'

如何快速加載表格並將其連接到完整記錄中? 謝謝您的幫助。



解決

Sergey 的原始答案有效但在前端循環引用失敗(這是 EFCore 所要求的)。 所以你必須告訴 Json 序列化器忽略向后引用。 工作代碼如下:

// Table2.cs 使用 System.Text.Json; 使用 System.Text.Json.Serializer; ... public int Id { 得到; 放; } public int Table2Column1 { get; 放; } public int Table2Column2 { 得到; 放; } public int Table2Column3 { 得到; 放; }

public int Table1Id { get; set; }
[JsonIgnore]
public Table1 Table1 { get; set; }
...

//Table1Controller.cs
...
// GET: api/table1/{id}
[HttpGet("{id}")]
public ActionResult<Table1> GeTable1ById(int id)
{
    return _context.Table1
                      .Include("Table2")
                      .Where(p => p.Id == id)
                      .FirstOrDefault();
}
...

您必須包含導航屬性,而不是包含表名。 此查詢應該有效:

var query =  _context.Table1
                .Include(t1 => t1.Column3)
                .Where(p => p.Id == id);

將您的操作更改為此:

HttpGet("{id}")]
public ActionResult<Table1> GeTable1ById(int id)
{
  
return _context.Table1
                      .Include(t=>t.Column3)
                      .Where(p => p.Id == id)
                      .FirstOrDefault();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM