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

Stella981
• 阅读 709

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,效果

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

 4,后记

  本人最近正在研究Abp框架,发现官方给的demo和文档只是交大家怎么使用,有些功能文档中并没有写明,所以必须通过查看源码来找到自己问题的解决方法。大家在abp开发中有什么遇到的问题或者有什么建议,欢迎大家留言。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
sql注入
反引号是个比较特别的字符,下面记录下怎么利用0x00SQL注入反引号可利用在分隔符及注释作用,不过使用范围只于表名、数据库名、字段名、起别名这些场景,下面具体说下1)表名payload:select\from\users\whereuser\_id1limit0,1;!(https://o
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
Oracle一张表中实现对一个字段不同值和总值的统计(多个count)
需求:统计WAIT\_ORDER表中的工单总数、未处理工单总数、已完成工单总数、未完成工单总数。表结构:为了举例子方便,WAIT\_ORDER表只有两个字段,分别是ID、STATUS,其中STATUS为工单的状态。1表示未处理,2表示已完成,3表示未完成总数。 SQL:  1.SELECT   2
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这