【问题标题】:Entity Framework Core Seeding 3 related entitiesEntity Framework Core Seeding 3 相关实体
【发布时间】:2018-02-27 07:02:55
【问题描述】:

我有 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();

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

任何帮助将不胜感激。

【问题讨论】:

  • 有什么关系?一个AccountGroup 到多个AccountCategory,然后一个AccountCategory 到多个Account
  • 是的,所以基本上,您只需重复您所做的并将AccountGroup 更改为Revenue.AccountCategory 并将AccountCategory 更改为AccountCategory.Account...我的意思是...这是相同的关系类型
  • 你还需要一个foreach( ChildType item IN ParentType.Children){}

标签: entity-framework seeding


【解决方案1】:

类似:

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

await _context.SaveChangesAsync();

【讨论】:

  • 完美运行。
猜你喜欢
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多