Entity Framework 核心播种 3 个相关实体

Entity Framework Core Seeding 3 related entities

我有 3 个相关的实体(帐户组到帐户类别再到帐户)。我正在尝试为数据库播种。下面的代码适用于播种帐户组和帐户类别,但我不确定如何播种下一级帐户。

Database Diagram

    var Revenue = new AccountGroup()
            {
                AccountGroupName = "Revenue",
                AccountCategories = new List<AccountCategory>()
                {
                    new AccountCategory { AccountCategoryName = "Base Business Build" },
                    new AccountCategory { AccountCategoryName = "Contract" },
                    new AccountCategory { AccountCategoryName = "Other" }
                }
            };

            _context.AccountGroups.Add(Revenue);

            _context.AccountCategories.AddRange(Revenue.AccountCategories);

            await _context.SaveChangesAsync();

基本业务构建类别中的帐户示例为: 项目工作, 垂直销售。

如有任何帮助,我们将不胜感激。

类似于:

foreach(AccountCategories singleCategories  in Revenue.AccountCategories)
{
    _context.Account.AddRange(singleCategories.Acount)
}

await _context.SaveChangesAsync();