ABP框架源码学习之修改默认数据库表前缀或表名称
1,源码
namespace Abp.Zero.EntityFramework
{
/// <summary>
/// Extension methods for <see cref="DbModelBuilder"/>.
/// </summary>
public static class AbpZeroDbModelBuilderExtensions
{
/// <summary>
/// Changes prefix for ABP tables (which is "Abp" by default).
/// Can be null/empty string to clear the prefix.
/// </summary>
/// <typeparam name="TTenant">The type of the tenant entity.</typeparam>
/// <typeparam name="TRole">The type of the role entity.</typeparam>
/// <typeparam name="TUser">The type of the user entity.</typeparam>
/// <param name="modelBuilder">Model builder.</param>
/// <param name="prefix">Table prefix, or null to clear prefix.</param>
/// <param name="schemaName">Schema name</param>
public static void ChangeAbpTablePrefix<TTenant, TRole, TUser>(this DbModelBuilder modelBuilder, string prefix, string schemaName = null)
where TTenant : AbpTenant<TUser>
where TRole : AbpRole<TUser>
where TUser : AbpUser<TUser>
{
prefix = prefix ?? ""; SetTableName<AuditLog>(modelBuilder, prefix + "AuditLogs", schemaName);
SetTableName<BackgroundJobInfo>(modelBuilder, prefix + "BackgroundJobs", schemaName);
SetTableName<Edition>(modelBuilder, prefix + "Editions", schemaName);
SetTableName<FeatureSetting>(modelBuilder, prefix + "Features", schemaName);
SetTableName<TenantFeatureSetting>(modelBuilder, prefix + "Features", schemaName);
SetTableName<EditionFeatureSetting>(modelBuilder, prefix + "Features", schemaName);
SetTableName<ApplicationLanguage>(modelBuilder, prefix + "Languages", schemaName);
SetTableName<ApplicationLanguageText>(modelBuilder, prefix + "LanguageTexts", schemaName);
SetTableName<NotificationInfo>(modelBuilder, prefix + "Notifications", schemaName);
SetTableName<NotificationSubscriptionInfo>(modelBuilder, prefix + "NotificationSubscriptions", schemaName);
SetTableName<OrganizationUnit>(modelBuilder, prefix + "OrganizationUnits", schemaName);
SetTableName<PermissionSetting>(modelBuilder, prefix + "Permissions", schemaName);
SetTableName<RolePermissionSetting>(modelBuilder, prefix + "Permissions", schemaName);
SetTableName<UserPermissionSetting>(modelBuilder, prefix + "Permissions", schemaName);
SetTableName<TRole>(modelBuilder, prefix + "Roles", schemaName);
SetTableName<Setting>(modelBuilder, prefix + "Settings", schemaName);
SetTableName<TTenant>(modelBuilder, prefix + "Tenants", schemaName);
SetTableName<UserLogin>(modelBuilder, prefix + "UserLogins", schemaName);
SetTableName<UserLoginAttempt>(modelBuilder, prefix + "UserLoginAttempts", schemaName);
SetTableName<TenantNotificationInfo>(modelBuilder, prefix + "TenantNotifications", schemaName);
SetTableName<UserNotificationInfo>(modelBuilder, prefix + "UserNotifications", schemaName);
SetTableName<UserOrganizationUnit>(modelBuilder, prefix + "UserOrganizationUnits", schemaName);
SetTableName<UserRole>(modelBuilder, prefix + "UserRoles", schemaName);
SetTableName<TUser>(modelBuilder, prefix + "Users", schemaName);
SetTableName<UserAccount>(modelBuilder, prefix + "UserAccounts", schemaName);
SetTableName<UserClaim>(modelBuilder, prefix + "UserClaims", schemaName);
SetTableName<RoleClaim>(modelBuilder, prefix + "RoleClaims", schemaName);
SetTableName<UserToken>(modelBuilder, prefix + "UserTokens", schemaName);
} internal static void SetTableName<TEntity>(this DbModelBuilder modelBuilder, string tableName, string schemaName)
where TEntity : class
{
if (schemaName == null)
{
modelBuilder.Entity<TEntity>().ToTable(tableName);
}
else
{
modelBuilder.Entity<TEntity>().ToTable(tableName, schemaName);
}
}
}
}
2,操作
public class WCHSOFTDbContext : AbpZeroDbContext<Tenant, Role, User, WCHSOFTDbContext>
{
public WCHSOFTDbContext(DbContextOptions<WCHSOFTDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ChangeAbpTablePrefix<Tenant, Role, User>("Wch");
base.OnModelCreating(modelBuilder);
}
}
3,效果
4,后记
本人最近正在研究Abp框架,发现官方给的demo和文档只是交大家怎么使用,有些功能文档中并没有写明,所以必须通过查看源码来找到自己问题的解决方法。大家在abp开发中有什么遇到的问题或者有什么建议,欢迎大家留言。