实体框架核心2.1中的种子关系数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实体框架核心2.1中的种子关系数据相关的知识,希望对你有一定的参考价值。

根据.NET核心文档(https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding),我可以在HasData中使用ModelBuilder播种数据。

但我需要播种关系数据,如:

public class ModuleComponent
{
        public int Id { get; set; }
        public int DisplayOrder { get; set; }
        public string ModuleName { get; set; }        
        public int Level { get; set; }        
        public int? ParentId { get; set; }
        public ModuleComponent Parent { get; set; }
        public ICollection<ModuleComponent> Children {get; set;}
}

在我创建Seeder类并从Program.cs调用它之前

namespace CRSApp.API.Data
{
    public class SeederModuleComponent
    {
        private readonly DataContext _context;

        public SeederModuleComponent(DataContext context)
        {
            _context = context;
        }

        public void Seeding()
        {
            if (!_context.ModuleComponent.Any())
            {
                // create Admin etc 
                var admin = new ModuleComponent
                {
                    DisplayOrder = 0,
                    ModuleName = "Admin",
                    Level = 0
                };

                var dashboard = new ModuleComponent
                {
                    DisplayOrder = 1,
                    ModuleName = "Dashboard",
                    Level = 0
                };

                var clients = new ModuleComponent
                {
                    DisplayOrder = 2,
                    ModuleName = "Clients",
                    Level = 0
                };

                var timesheet = new ModuleComponent
                {
                    DisplayOrder = 3,
                    ModuleName = "Time Sheet",
                    Level = 0
                };

                var billings = new ModuleComponent
                {
                    DisplayOrder = 3,
                    ModuleName = "Billing",
                    Level = 0
                };

                var reports = new ModuleComponent
                {
                    DisplayOrder = 4,
                    ModuleName = "Reports",
                    Level = 0
                };

                _context.ModuleComponent.Add(admin);
                _context.ModuleComponent.Add(dashboard);
                _context.ModuleComponent.Add(clients);
                _context.ModuleComponent.Add(timesheet);
                _context.ModuleComponent.Add(billings);
                _context.ModuleComponent.Add(reports);

                //ADMIN
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Organisation", Level = 1, DisplayOrder = 0, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "User Group", Level = 1, DisplayOrder = 1, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Users", Level = 1, DisplayOrder = 2, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Entitlements", Level = 1, DisplayOrder = 3, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Staff Leave Approval", Level = 1, DisplayOrder = 4, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Settings", Level = 1, DisplayOrder = 5, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Department Productivity", Level = 1, DisplayOrder = 6, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Staff Productivity Chart", Level = 1, DisplayOrder = 7, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Job Profitability", Level = 1, DisplayOrder = 8, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Activity Codes", Level = 1, DisplayOrder = 9, Parent = admin });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Master Password", Level = 1, DisplayOrder = 10, Parent = admin });

                //Dashboard
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Summary of Active Clients", Level = 1, DisplayOrder = 0, Parent = dashboard });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Practice Work In Progress", Level = 1, DisplayOrder = 1, Parent = dashboard });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Employee Productivity", Level = 1, DisplayOrder = 2, Parent = dashboard });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Leave", Level = 1, DisplayOrder = 3, Parent = dashboard });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Staff Default Rates", Level = 1, DisplayOrder = 4, Parent = dashboard });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Activity Codes", Level = 1, DisplayOrder = 5, Parent = dashboard });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Change Password", Level = 1, DisplayOrder = 6, Parent = dashboard });

                // Clients
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Clients Active Case", Level = 1, DisplayOrder = 0, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Client Personal", Level = 1, DisplayOrder = 1, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Client Company", Level = 1, DisplayOrder = 2, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "My Clients Profile", Level = 1, DisplayOrder = 3, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Clients Profile", Level = 1, DisplayOrder = 4, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Hourly Rate Assigned", Level = 1, DisplayOrder = 5, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Edit Hourly Rate Assigned", Level = 1, DisplayOrder = 6, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Reassigning Clients", Level = 1, DisplayOrder = 7, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "WIP Pre Billed", Level = 1, DisplayOrder = 8, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "My Client Billing History", Level = 1, DisplayOrder = 9, Parent = clients });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Client Billing History", Level = 1, DisplayOrder = 10, Parent = clients });

                //Timesheet
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Create New Timesheet", Level = 1, DisplayOrder = 0, Parent = timesheet });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "My Timesheets", Level = 1, DisplayOrder = 1, Parent = timesheet });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Employee Timesheets", Level = 1, DisplayOrder = 2, Parent = timesheet });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Approve/Reject Timesheets", Level = 1, DisplayOrder = 3, Parent = timesheet });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Create Expense Sheet", Level = 1, DisplayOrder = 4, Parent = timesheet });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "My Expense Sheets", Level = 1, DisplayOrder = 5, Parent = timesheet });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Employee Expense Sheets", Level = 1, DisplayOrder = 6, Parent = timesheet });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Approve/Reject Expense Sheet", Level = 1, DisplayOrder = 7, Parent = timesheet });

                // Billings
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Create New Bill", Level = 1, DisplayOrder = 0, Parent = billings });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "My Billing", Level = 1, DisplayOrder = 1, Parent = billings });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Billing History", Level = 1, DisplayOrder = 2, Parent = billings });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Approve/Reject Bill", Level = 1, DisplayOrder = 3, Parent = billings });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "My Potentials", Level = 1, DisplayOrder = 4, Parent = billings });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Create New Potential", Level = 1, DisplayOrder = 5, Parent = billings });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Potentials", Level = 1, DisplayOrder = 6, Parent = billings });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "My Batch Billing", Level = 1, DisplayOrder = 7, Parent = billings });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Batch Billing", Level = 1, DisplayOrder = 8, Parent = billings });

                //Reports
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Timesheet Analysis Report", Level = 1, DisplayOrder = 0, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "WIP Client Report", Level = 1, DisplayOrder = 1, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "WIP Practice Report", Level = 1, DisplayOrder = 2, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "My Report", Level = 1, DisplayOrder = 3, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Employee Report", Level = 1, DisplayOrder = 4, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "My Client Analysis Report", Level = 1, DisplayOrder = 5, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Client Analysis Report", Level = 1, DisplayOrder = 6, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Create Summary Remuneration Report", Level = 1, DisplayOrder = 7, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Summary Remun. Report", Level = 1, DisplayOrder = 8, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Create Detailed Remun. Report", Level = 1, DisplayOrder = 9, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Detailed Remun. Report", Level = 1, DisplayOrder = 10, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Create Summary Expense Report", Level = 1, DisplayOrder = 11, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Summary Expense Report", Level = 1, DisplayOrder = 12, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "Create Detailed Expense Report", Level = 1, DisplayOrder = 13, Parent = reports });
                _context.ModuleComponent.Add(new ModuleComponent { ModuleName = "All Detailed Expense Report", Level = 1, DisplayOrder = 14, Parent = reports });

                _context.SaveChanges();
            }
        }
    }
}

如何处理EF Core 2.1?因为我们可能不知道对象的parentId。

谢谢

答案

对于HasData(),你需要提供ModuleComponent[]和显式集IdParentId

这是一个演示代码:

    public class SeederData
{
    public static ModuleComponent[] SeederModuleArray()
    {
        int index = 1;
        var result = new List<ModuleComponent>();
        var admin = new ModuleComponent
        {
            Id = index,
            DisplayOrder = 0,
            ModuleName = "Admin",
            Level = 0
        };
        result.Add(admin);
        var dashboard = new ModuleComponent
        {
            Id = ++index,
            DisplayOrder = 1,
            ModuleName = "Dashboard",
            Level = 0
        };
        result.Add(dashboard);
        var Organisation = new ModuleComponent
        {
            Id = ++index,
            ModuleName = "Organisation",
            Level = 1,
            DisplayOrder = 0,
            ParentId = admin.Id
        };
        result.Add(Organisation);
        var Leave = new ModuleComponent
        {
            Id = ++index,
            ModuleName = "Leave",
            Level = 1,
            DisplayOrder = 3,
            ParentId = dashboard.Id
        };
        result.Add(Leave);
        return result.ToArray();
    }
}

ModuleComponent[]中使用DbContext

            builder.Entity<ModuleComponent>().HasData(
            SeederData.SeederModuleArray()
        );

以上是关于实体框架核心2.1中的种子关系数据的主要内容,如果未能解决你的问题,请参考以下文章

Swift 核心数据关系和种子数据

带有实体框架的 ASP.NET MVC Core 项目中的种子角色

理解实体框架核心外键关系

实体框架核心关系问题(代码优先) - 重复列

是否可以在实体框架(代码优先)的种子方法中添加两个表的数据?

为啥实体框架核心加载实体的关系而不添加包含