【问题标题】:.NET 5 Core Entity Framework Controller w/OData Return Child Object with a Child Object.NET 5 Core Entity Framework Controller w/OData 返回带有子对象的子对象
【发布时间】:2021-11-08 07:36:39
【问题描述】:

CoreMasterAccount 有许多 CoreSubAccount。 CoreSubAccounts 有许多 CoreCircuits。

目前,https://localhost:5001/odata/CoreMasterAccounts?$top=2&$expand=CoreSubAccounts($expand=CoreCircuits) 返回:

"value": [{
        "MasterAccountId": 1,
        "ForeignKey": "foreignkey1",
        "CustomerName": "Nunya",
        "CoreSubAccounts": [{
                "SubAccountId": 1,
                "MasterAccountId": 1,
                "Btn": "630-323-8400",
                "CustomerName": "Nunya1",
                "CoreCircuits": []
            }, {
                "SubAccountId": 2,
                "MasterAccountId": 1,
                "Btn": "630-920-1100",
                "CustomerName": "Nunya2",
                "CoreCircuits": []
            }, {
                "SubAccountId": 3,
                "MasterAccountId": 1,
                "Btn": "708-444-2100",
                "CustomerName": "Nunya3",
                "CoreCircuits": []
            },

我也希望 CoreCircuits 能够填充。这是通过控制器发生的还是与模型有关?仅供参考,CoreCircuits 与 CoreSubAccounts 具有多对一关系,其结构与 CoreSubAccounts 与 CoreMasterAccounts 多对一关系的结构相同,工作正常。

CoreMasterAccount 控制器:

public async Task<ActionResult<IEnumerable<CoreSubAccount>>> GetCoreSubAccounts()
    {
        return await _context.CoreSubAccounts
           .Include(e => e.CoreCircuits)
           .ToListAsync();
    }


 public async Task<ActionResult<IEnumerable<CoreMasterAccount>>> GetCoreMasterAccounts()
    {
        return await _context.CoreMasterAccounts
            .Include(e => e.CoreSubAccounts)
            .Include(e => e.CoreInvoices)
            .Include(e => e.TxnManualTransactions)
            .ToListAsync();
    }

CoreSubAccount 控制器:

public async Task<ActionResult<IEnumerable<CoreSubAccount>>> GetCoreSubAccounts()
    {
        return await _context.CoreSubAccounts
           .Include(e => e.CoreCircuits)
           .ToListAsync();
    }

【问题讨论】:

    标签: c# asp.net-core entity-framework-core odata


    【解决方案1】:

    像这样更改您的 GetCoreSubAccounts 代码:

    public async Task<ActionResult<IEnumerable<CoreSubAccount>>> GetCoreSubAccounts()
        {
            return await _context.CoreSubAccounts
               .Include(e => e.CoreCircuits)
                   .ThenInclude(e => e.CoreCircuits)
               .ToListAsync();
        }
    

    您可以在此处找到更多信息和示例:https://entityframeworkcore.com/querying-data-include-theninclude

    【讨论】:

    • 非常感谢!在我的研究时间里,我从未遇到过那个网站,这真是令人难以置信。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多