第一篇:来自 .net 开发菜鸟 博主的文章:https://www.cnblogs.com/dotnet261010/p/8035213.html
第二篇:来自 JustYong 博主的文章:https://www.cnblogs.com/JustYong/p/5970683.html
感谢两位博主贡献精彩文章,感谢分享。
自己公司使用的开发框架,每次使用都要手动的把一些公共的数据添加进去,感觉很麻烦,所以就查了一下 “ 如何填充种子数据” ,看了以上的文章,很受益。
在Migrations文件夹下的 数据迁移配置文件 Configuration.cs 下的 Seed()函数中添加种子数据,代码如下,亲测针对本公司框架使用无误:
namespace ORM.Migrations { using Modules; using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; public sealed class Configuration : DbMigrationsConfiguration<ORM.MyDbContext> { public Configuration() { AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; ContextKey = "ORM.MyDbContext"; } protected override void Seed(ORM.MyDbContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. var pId = Guid.NewGuid(); int orderNumber = 999; var sysMenuSet = new Menu() { Id = pId, ParentId = null, Name = "系统设置", Url = null, IconClass = null, OrderNumber = orderNumber }; var menuSet = new Menu() { Id = Guid.NewGuid(), ParentId = pId, Name = "菜单设置", Url = "Menu/Index", IconClass = null, OrderNumber = orderNumber }; var dicitemSet = new Menu() { Id = Guid.NewGuid(), ParentId = pId, Name = "字典设置", Url = "Dic/Index", IconClass = null, OrderNumber = orderNumber }; var userSet = new Menu() { Id = Guid.NewGuid(), ParentId = pId, Name = "账户设置", Url = "User/Index", IconClass = null, OrderNumber = orderNumber }; var empolyeeSet = new Menu() { Id = Guid.NewGuid(), ParentId = pId, Name = "用户管理", Url = "Employee/Index", IconClass = null, OrderNumber = orderNumber }; var roleSet = new Menu() { Id = Guid.NewGuid(), ParentId = pId, Name = "角色管理", Url = "Role/Index", IconClass = null, OrderNumber = orderNumber }; var departmentSet = new Menu() { Id = Guid.NewGuid(), ParentId = pId, Name = "部门管理", Url = "Department/Index", IconClass = null, OrderNumber = orderNumber }; var tagSet = new Menu() { Id = Guid.NewGuid(), ParentId = pId, Name = "标签管理", Url = "Tag/Index", IconClass = null, OrderNumber = orderNumber }; var funcSet = new Menu() { Id = Guid.NewGuid(), ParentId = pId, Name = "权限管理", Url = "Func/Index", IconClass = null, OrderNumber = orderNumber }; context.Menus.Add(sysMenuSet); context.Menus.Add(menuSet); context.Menus.Add(dicitemSet); context.Menus.Add(roleSet); context.Menus.Add(userSet); context.Menus.Add(empolyeeSet); context.Menus.Add(departmentSet); context.Menus.Add(tagSet); context.Menus.Add(funcSet); base.Seed(context); } } }