ABP框架源码学习之修改默认数据库表前缀或表名称 1,源码
1 namespace Abp.Zero.EntityFramework 2 { 3 /// <summary> 4 /// Extension methods for <see cref="DbModelBuilder"/>. 5 /// </summary> 6 public static class AbpZeroDbModelBuilderExtensions 7 { 8 /// <summary> 9 /// Changes prefix for ABP tables (which is "Abp" by default). 10 /// Can be null/empty string to clear the prefix. 11 /// </summary> 12 /// <typeparam>The type of the tenant entity.</typeparam> 13 /// <typeparam>The type of the role entity.</typeparam> 14 /// <typeparam>The type of the user entity.</typeparam> 15 /// <param>Model builder.</param> 16 /// <param>Table prefix, or null to clear prefix.</param> 17 /// <param>Schema name</param> 18 public static void ChangeAbpTablePrefix<TTenant, TRole, TUser>(this DbModelBuilder modelBuilder, string prefix, string schemaName = null) 19 where TTenant : AbpTenant<TUser> 20 where TRole : AbpRole<TUser> 21 where TUser : AbpUser<TUser> 22 { 23 prefix = prefix ?? ""; 24 25 SetTableName<AuditLog>(modelBuilder, prefix + "AuditLogs", schemaName); 26 SetTableName<BackgroundJobInfo>(modelBuilder, prefix + "BackgroundJobs", schemaName); 27 SetTableName<Edition>(modelBuilder, prefix + "Editions", schemaName); 28 SetTableName<FeatureSetting>(modelBuilder, prefix + "Features", schemaName); 29 SetTableName<TenantFeatureSetting>(modelBuilder, prefix + "Features", schemaName); 30 SetTableName<EditionFeatureSetting>(modelBuilder, prefix + "Features", schemaName); 31 SetTableName<ApplicationLanguage>(modelBuilder, prefix + "Languages", schemaName); 32 SetTableName<ApplicationLanguageText>(modelBuilder, prefix + "LanguageTexts", schemaName); 33 SetTableName<NotificationInfo>(modelBuilder, prefix + "Notifications", schemaName); 34 SetTableName<NotificationSubscriptionInfo>(modelBuilder, prefix + "NotificationSubscriptions", schemaName); 35 SetTableName<OrganizationUnit>(modelBuilder, prefix + "OrganizationUnits", schemaName); 36 SetTableName<PermissionSetting>(modelBuilder, prefix + "Permissions", schemaName); 37 SetTableName<RolePermissionSetting>(modelBuilder, prefix + "Permissions", schemaName); 38 SetTableName<UserPermissionSetting>(modelBuilder, prefix + "Permissions", schemaName); 39 SetTableName<TRole>(modelBuilder, prefix + "Roles", schemaName); 40 SetTableName<Setting>(modelBuilder, prefix + "Settings", schemaName); 41 SetTableName<TTenant>(modelBuilder, prefix + "Tenants", schemaName); 42 SetTableName<UserLogin>(modelBuilder, prefix + "UserLogins", schemaName); 43 SetTableName<UserLoginAttempt>(modelBuilder, prefix + "UserLoginAttempts", schemaName); 44 SetTableName<TenantNotificationInfo>(modelBuilder, prefix + "TenantNotifications", schemaName); 45 SetTableName<UserNotificationInfo>(modelBuilder, prefix + "UserNotifications", schemaName); 46 SetTableName<UserOrganizationUnit>(modelBuilder, prefix + "UserOrganizationUnits", schemaName); 47 SetTableName<UserRole>(modelBuilder, prefix + "UserRoles", schemaName); 48 SetTableName<TUser>(modelBuilder, prefix + "Users", schemaName); 49 SetTableName<UserAccount>(modelBuilder, prefix + "UserAccounts", schemaName); 50 SetTableName<UserClaim>(modelBuilder, prefix + "UserClaims", schemaName); 51 SetTableName<RoleClaim>(modelBuilder, prefix + "RoleClaims", schemaName); 52 SetTableName<UserToken>(modelBuilder, prefix + "UserTokens", schemaName); 53 } 54 55 internal static void SetTableName<TEntity>(this DbModelBuilder modelBuilder, string tableName, string schemaName) 56 where TEntity : class 57 { 58 if (schemaName == null) 59 { 60 modelBuilder.Entity<TEntity>().ToTable(tableName); 61 } 62 else 63 { 64 modelBuilder.Entity<TEntity>().ToTable(tableName, schemaName); 65 } 66 } 67 } 68 }