Seeding relationship data in Entity Framework Core 2.1

According to .NET Core docs (https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding), I could seed the data with HasData in ModelBuilder.

But I need to seed a relational data like:

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;}
}

Before I did create Seeder class and calling it from 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();
            }
        }
    }
}

How to do with EF Core 2.1 ? As we might not know the parentId of object.

Thank you

1 answer

  • answered 2018-07-11 06:40 Tao Zhou

    For HasData(), you will need to provider ModuleComponent[] and explicit set Id and ParentId.

    Here is a demo code:

        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();
        }
    }
    

    Use ModuleComponent[] in DbContext

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