起因:早上偶然看见一篇文章说是ef core(2.x)使用种子数据,主表子表迁移时候,正常情况下说是无法迁移成功,索性就试试,结果是和ef6的一样,没感觉有什么大的区别。一切OK,见下面内容。
1.首先我们依旧按照惯常的方式创建几个Entity实体对象,如下图,几个实体应该不用说明什么意思,很显而易见,最下面的 UserRolePermission是role和permission的中间表,(多对多拆分一对一)
那么这里自然的就包含了主表和子表之间的主外键引用关系了。
2.然后依旧按照惯常方式创建一个Entity的实体对象的EntityTypeConfiguration对象,如下图:其中的 SeedData文件夹中的类是 EntityTypeBuilder的扩展类(看名字也知道了),主要放的就是种子数据了。
其中的 SeedData下的类内容:
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 EntityTypeBuilder<PermissionSeed> builder) { var permissions = new List<PermissionSeed>() { 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<UserRole>() { 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<UserRolePermission>() { 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<UserExtension>() { 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<User>() { 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; } }