起因:早上偶然看见一篇文章说是ef core(2.x)使用种子数据,主表子表迁移时候,正常情况下说是无法迁移成功,索性就试试,结果是和ef6的一样,没感觉有什么大的区别。一切OK,见下面内容。
1.首先我们依旧按照惯常的方式创建几个Entity实体对象,
如下图,几个实体应该不用说明什么意思,很显而易见,最下面的 UserRolePermission是role和permission的中间表,(多对多拆分一对一)
那么这里自然的就包含了主表和子表之间的主外键引用关系了。
2.然后依旧按照惯常方式创建一个Entity的实体对象的EntityTypeConfiguration对象,如下图:其中的 SeedData文件夹中的类是 EntityTypeBuilder的扩展类(看名字也知道了),主要放的就是种子数据了。
其中的 SeedData下的类内容:
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
public static class EntityTypeBuilderExtensions { #region permissionIds private static Guid permissionId_01 = CombGuid.NewGuid(); private static Guid permissionId_02 = CombGuid.NewGuid(); private static Guid permissionId_03 = CombGuid.NewGuid(); private static Guid permissionId_04 = CombGuid.NewGuid(); private static Guid permissionId_05 = CombGuid.NewGuid(); private static Guid permissionId_06 = CombGuid.NewGuid(); private static Guid permissionId_07 = CombGuid.NewGuid(); private static Guid permissionId_08 = CombGuid.NewGuid(); private static Guid permissionId_09 = CombGuid.NewGuid(); private static Guid permissionId_10 = CombGuid.NewGuid(); private static Guid permissionId_11 = CombGuid.NewGuid(); private static Guid permissionId_12 = CombGuid.NewGuid(); private static Guid permissionId_13 = CombGuid.NewGuid(); #endregion #region roleId private static Guid roleId_01 = CombGuid.NewGuid(); private static Guid roleId_02 = CombGuid.NewGuid(); private static Guid roleId_03 = CombGuid.NewGuid(); #endregion #region userExtensionId private static Guid userExtensionId_01 = CombGuid.NewGuid(); #endregion public static EntityTypeBuilder HasDataPermissionSeed(this EntityTypeBuilderbuilder) { var permissions = new List () { new PermissionSeed(){ ID = permissionId_01,ControllerName="",ActionName="",IsDisabled=0,OrderIndex=0,Description=@"系统管理" }, new PermissionSeed(){ ID = permissionId_02,ControllerName="",ActionName="",IsDisabled=0,OrderIndex=1,Description=@"组织架构" }, new PermissionSeed(){ ID = permissionId_03,ControllerName="",ActionName="",IsDisabled=0,OrderIndex=2,Description=@"内容管理" }, new PermissionSeed(){ ID = permissionId_04,ControllerName="",ActionName="",IsDisabled=0,OrderIndex=3,Description=@"统计报表" }, new PermissionSeed(){ ID = permissionId_05,ParentPermissionID=permissionId_01,ControllerName="SystemSetting",ActionName="Index",IsDisabled=0,OrderIndex=0,Description=@"系统设置" }, new PermissionSeed(){ ID = permissionId_06,ParentPermissionID=permissionId_01,ControllerName="SystemSetting",ActionName="Other",IsDisabled=0,OrderIndex=1,Description=@"其他设置" }, new PermissionSeed(){ ID = permissionId_07,ParentPermissionID=permissionId_02,ControllerName="PermissionManager",ActionName="Index",IsDisabled=0,OrderIndex=0,Description=@"权限管理" }, new PermissionSeed(){ ID = permissionId_08,ParentPermissionID=permissionId_02,ControllerName="RoleManager",ActionName="Index",IsDisabled=0,OrderIndex=1,Description=@"角色管理" }, new PermissionSeed(){ ID = permissionId_09,ParentPermissionID=permissionId_02,ControllerName="UserManager",ActionName="Index",IsDisabled=0,OrderIndex=2,Description=@"用户管理" }, new PermissionSeed(){ ID = permissionId_10,ParentPermissionID=permissionId_03,ControllerName="ArticleManager",ActionName="Index",IsDisabled=0,OrderIndex=0,Description=@"文章管理" }, new PermissionSeed(){ ID = permissionId_11,ParentPermissionID=permissionId_03,ControllerName="VideoManager",ActionName="Index",IsDisabled=0,OrderIndex=1,Description=@"视频管理" }, new PermissionSeed(){ ID = permissionId_12,ParentPermissionID=permissionId_04,ControllerName="TongJiManager",ActionName="Index",IsDisabled=0,OrderIndex=0,Description=@"统计管理" }, new PermissionSeed(){ ID = permissionId_13,ParentPermissionID=permissionId_04,ControllerName="BaoBiaoManager",ActionName="Index",IsDisabled=0,OrderIndex=0,Description=@"报表管理" }, }; builder.HasData(permissions.ToArray()); return builder; } public static EntityTypeBuilder HasDataRole(this EntityTypeBuilder builder) { var roles = new List () { new UserRole(){ID = roleId_01,RoleName = @"超级管理员",IsDisabled = Entity.Enums.IsDisabled.UnDisable,CreateTime = DateTime.Now}, new UserRole(){ID = roleId_02,RoleName = @"管理员",IsDisabled = Entity.Enums.IsDisabled.UnDisable,CreateTime = DateTime.Now}, new UserRole(){ID = roleId_03,RoleName = @"金笔写手",IsDisabled = Entity.Enums.IsDisabled.UnDisable,CreateTime = DateTime.Now}, }; builder.HasData(roles.ToArray()); return builder; } public static EntityTypeBuilder HasDataRolePermission(this EntityTypeBuilder builder) { var rolePermissions = new List () { new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_01,UserRoleID = roleId_01} , new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_02,UserRoleID = roleId_01} , new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_03,UserRoleID = roleId_01} , new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_04,UserRoleID = roleId_01} , new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_05,UserRoleID = roleId_01} , new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_06,UserRoleID = roleId_01} , new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_07,UserRoleID = roleId_01} , new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_08,UserRoleID = roleId_01} , new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_09,UserRoleID = roleId_01} , new UserRolePermission(){ID = CombGuid.NewGuid(),PermissionSeedID = permissionId_10,UserRoleID = roleId_01} }; builder.HasData(rolePermissions.ToArray()); return builder; } public static EntityTypeBuilder HasDataUserExtension(this EntityTypeBuilder builder) { var userExtensions = new List () { new UserExtension() { ID =userExtensionId_01, AccountBalance=0, Integral = 0, ReadCoin = 1000 } }; builder.HasData(userExtensions.ToArray()); return builder; } public static EntityTypeBuilder HasDataUser(this EntityTypeBuilder builder) { var users = new List () { new User(){ ID = CombGuid.NewGuid(),CreateTime = DateTime.Now,Gender = Entity.Enums.GenderEnum.Man,HeadImage="", IsDisabled = Entity.Enums.IsDisabled.UnDisable,LoginAccount = "thomas",LoginPassword="123qwe",NickName="thomas", Remark = "seed初始化种子数据",Telphone="15251820632",UserRoleID = roleId_01,UserEctensionID=userExtensionId_01 }, new User(){ ID = CombGuid.NewGuid(),CreateTime = DateTime.Now,Gender = Entity.Enums.GenderEnum.Man,HeadImage="", IsDisabled = Entity.Enums.IsDisabled.UnDisable,LoginAccount = "thomas",LoginPassword="123qwe",NickName="thomas", Remark = "seed初始化种子数据",Telphone="15251820632",UserRoleID = roleId_01,UserEctensionID=userExtensionId_01 }, }; builder.HasData(users.ToArray()); return builder; } }
然后我们只需要在对应的 EntityTypeConfiguration直接使用扩展方法就行了,如下图其中一个示例:
3.那么至此,我们该创建的全创建完了,其他的,至于上下文对象(dbContext的创建),项目的创建(我这里创建的是一个web api项目),就省略了,,很简单。
4.开始我们的数据迁移操作:(我这里使用的sqlServer数据库)
说个题外话:我们在迁移的时候,可以在,DbContextOptionsBuilder (builder)builder.UseSqlServer这个扩展的时候,可以指定 migrations文件生成的所在目录,(当然这里会提示你事先需要实现IDesignTimeDbContextFactory这个接口,,比较简单,省略)
比如我直接指定生成在我的webapi的(presentation层,这也很合理,假设我如果使用的其他的数据库,比如sqlLite,这个builder就不是UserSQlServer了,是依据实际应用而定的)。
如图:
迁移: nuget 输入
add-migration init //不区分大小写,,哪来那么多要求,,
此时执行结果如下图:
然后看看我们项目里生成了啥(webapi)
5.目前我们成功生成了 迁移所需的迁移文件,但是还没迁移,这里如果 上下文没有设置 自动迁移的话,需要手动执行 updata-database去迁移生成数据库,
设置自动迁移:
或直接手动,继续在 nuget输入 updata-database即可,
看我们呢执行的结果:
PermissionSeed表:
User表:
Role表:
role和权限的中间表 rolePermission表:
再看看我们的主外键(其中一个):
一切OK,没有出现说无法正常初始化的问题,