【发布时间】:2018-02-27 07:02:55
【问题描述】:
我有 3 个相关的实体(帐户组到帐户类别到帐户)。我正在尝试为数据库播种。下面的代码适用于播种帐户组和帐户类别,但我不确定如何播种下一个级别,即帐户。
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){}