根据.NET Core文档(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。
谢谢
答案 0 :(得分:2)
对于HasData()
,您将需要提供ModuleComponent[]
并提供显式集Id
和ParentId
。
这是演示代码:
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()
);