ABP框架源码学习之修改默认数据库表前缀或表名称

Posted yanglang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 name="TTenant">The type of the tenant entity.</typeparam>
13         /// <typeparam name="TRole">The type of the role entity.</typeparam>
14         /// <typeparam name="TUser">The type of the user entity.</typeparam>
15         /// <param name="modelBuilder">Model builder.</param>
16         /// <param name="prefix">Table prefix, or null to clear prefix.</param>
17         /// <param name="schemaName">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 }

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开发中有什么遇到的问题或者有什么建议,欢迎大家留言。

以上是关于ABP框架源码学习之修改默认数据库表前缀或表名称的主要内容,如果未能解决你的问题,请参考以下文章

abp 修改abp.zero的实体映射类,使生成的表和字段为大写状态

django学习之- Models笔记

更改 ABP.IO 的表前缀和架构

[Architect] Abp 框架原理解析 UnitOfWork

Android源码学习之六——ActivityManager框架解析

[Architect] Abp 框架原理解析 UnitOfWork