【问题标题】:.NET 5 API with Entity Framework One to Many Relationship Not Working.NET 5 API 与实体框架一对多关系不起作用
【发布时间】:2021-11-08 04:49:53
【问题描述】:

一个 CoreMasterAccount 有许多 CoreSubAccount。两者有一个共同的 MasterAccountId。 CoreSubAccount 有它自己唯一的 SubAccountId。 API json 响应将显示 SubAccounts[] 但数组为空。我做了一些调试工作,发现这不是 JSONNewtonsoft 序列化的问题。没有返回任何 CoreSubAccounts。在这个方面不知所措。

CoreMasterAccount 模型

public partial class CoreMasterAccount
{
    public CoreMasterAccount()
    {
        CoreInvoices = new HashSet<CoreInvoice>();
        CoreSubAccounts = new HashSet<CoreSubAccount>();
        TxnManualTransactions = new HashSet<TxnManualTransaction>();
    }
    [Key]
    public long MasterAccountId { get; set; }
    public string ForeignKey { get; set; }
    public string CustomerName { get; set; }

    public virtual ICollection<CoreInvoice> CoreInvoices { get; set; }
    public virtual ICollection<CoreSubAccount> CoreSubAccounts { get; set; }
    public virtual ICollection<TxnManualTransaction> TxnManualTransactions { get; set; }
}

CoreSubAccount 模型

public partial class CoreSubAccount
{
    public CoreSubAccount()
    {
        CoreCircuits = new HashSet<CoreCircuit>();
    }
    [Key]
    public int SubAccountId { get; set; }
    public long MasterAccountId { get; set; }
    public string Btn { get; set; }
    public string CustomerName { get; set; }
    
    public virtual CoreMasterAccount MasterAccount { get; set; }
    public virtual ICollection<CoreCircuit> CoreCircuits { get; set; }
}

DBContext OnModelCreating:

modelBuilder.Entity<CoreSubAccount>(entity =>
        {
            entity.HasKey(e => e.SubAccountId)
                .HasName("PK_SubAccounts");

            entity.ToTable("core_SubAccounts");

            entity.HasIndex(e => e.Btn, "IX_core_SubAccounts")
                .IsUnique();
            .
            .
            .
            entity.HasOne(d => d.MasterAccount)
                .WithMany(p => p.CoreSubAccounts)
                .HasForeignKey(d => d.MasterAccountId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_core_SubAccounts_core_MasterAccounts")
                .IsRequired();
        });

JSON 响应

{
"@odata.context": "https://localhost:5001/odata/$metadata#CoreMasterAccounts(CoreSubAccounts())",
"value": [{
        "MasterAccountId": 1,
        "ForeignKey": "X5202",
        "CustomerName": "Nunya",
        "CoreSubAccounts": []
    }
]

}

编辑: CoreMasterAccount 控制器:

    public async Task<ActionResult<IEnumerable<CoreMasterAccount>>> GetCoreMasterAccounts()
    {
        return await _context.CoreMasterAccounts.ToListAsync();
    }

【问题讨论】:

  • 您能否展示如何使用 EF 查询数据?如果您没有包含 CoreSubAccounts,那么它可能无法正常工作。

标签: asp.net-core entity-framework-core odata .net-5


【解决方案1】:

您在查询主帐户时可能没有包含 CoreSubAccounts。如果没有您用来查询 CoreMasterAccounts 的实际代码,我只能质疑 Include 是问题所在。尝试以这种方式编写查询以获取 CoreMasterAccounts:

return dbContext.CoreMasterAccounts
.Include(e => e.CoreSubAccounts)
.ToList();

【讨论】:

  • 我将使用 CoreMasterAccounts 控制器信息更新帖子。添加没有采取 - 我可能格式不正确。
  • 您没有在控制器中添加包含?如果你只是做 _context.CoreMasterAccounts.ToListAsync();它不包括相关实体。
  • 知道了,我没有删除“await”运算符,因此您的代码最初无法正常工作。我删除了它,它现在正在工作。所以,这个调用不再是同步的。我将如何将其修改为异步?
  • 是的,我现在得到:严重性代码描述项目文件行抑制状态警告 CS1998 此异步方法缺少“等待”运算符,将同步运行。考虑使用 'await' 运算符来等待非阻塞 API 调用,或使用 'await Task.Run(...)' 在后台线程上执行 CPU 密集型工作。 xclutel-api C:\repository\xclutel-api\xclutel-api\Controllers\CoreMasterAccountsController.cs 27 活动
  • 解决方法:return await dbContext.CoreMasterAccounts .Include(e => e.CoreSubAccounts) .ToListAsync();
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-01
  • 2016-09-21
  • 1970-01-01
  • 2018-02-15
  • 2013-11-24
相关资源
最近更新 更多