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

原文地址:https://www.cnblogs.com/yanglang/p/9790110.html

时间: 2024-10-25 22:56:41

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

Android 4.3 源码下如何修改默认输入法

应项目(Android 4.3)需求需要修改默认输入法,本来想的是,这个应该很简单的撒.随google之-,发现网上主要罗列出以下这种方法: 在 frameworks\base\core\res\res\values\config.xml 添加一个属性:      <!--leo add-->    <string name="config_default_input_method">com.android.inputmethod.pinyin/.PinyinI

EventBus3.0 组件通信框架源码学习总结

一.初始化 EventBus的初始化虽然用了单例模式,但是构造方法居然是public修饰符,可能是应对项目中的多线操作. //单例模式,针对并发情况进行了双层判断 public static EventBus getDefault() { if (defaultInstance == null) { synchronized (EventBus.class) { if (defaultInstance == null) { defaultInstance = new EventBus(); }

Bottle 框架源码学习 三

def run(app=None, server='wsgiref', host='127.0.0.1', port=8080,         interval=1, reloader=False, quiet=False, plugins=None,         debug=None, **kargs): 今天要学习一下bottle里是怎样打印debug信息的 run函数的倒数第二个参数是debug,默认为None try:     if debug is not None: _debu

Bottle 框架源码学习 二

上一篇简单分析了route的基本用法 本篇分析一下run函数的运行原理 def run(app=None, server='wsgiref', host='127.0.0.1', port=8080,         interval=1, reloader=False, quiet=False, plugins=None,         debug=None, **kargs):          if NORUN: return     if reloader and not os.env

PHP框架CodeIgniter CI框架源码学习笔记-index.php一切的入口

CI框架(CodeIgniter)的基本执行流程图以备参考: index.php作为CI框架的入口文件,源码阅读,自然由此开始.

Bottle 框架源码学习 一

# -*- coding=utf-8 -*- from bottle import route, run, template,Bottle app = Bottle() @route("/hello/<name>") def index(name):     return template("<b>Hello, {{name}}</b>", name=name) run(app, host="localhost"

ABP框架源码中的Linq扩展方法

文件目录:aspnetboilerplate-dev\aspnetboilerplate-dev\src\Abp\Collections\Extensions\EnumerableExtensions.cs using System; using System.Collections.Generic; using System.Linq; namespace Abp.Collections.Extensions { /// <summary> /// Extension methods for

CI框架源码学习笔记6——Config.php

接着上一节往下,我们这一节来看看配置类Config.php,对应手册内容http://codeigniter.org.cn/user_guide/libraries/config.html. class CI_Config { //所有已经加载的配置项组成的数组 public $config = array(); //所有已经加载的配置文件组成的数组 public $is_loaded = array(); //用来搜索配置文件的路径数组 public $_config_paths = arra

mybatis框架源码学习

转:来自https://my.oschina.net/u/1458864/blog/293659 摘要:初始化mybatis,所有的配置都在configuation 对象中使用mybatis,从sqlsessionfactory 工厂中获取sqlsession,从configuation对象中获取mapper对象,并返回结果,mybatis在加载mapper的时候对mapper接口的注解进行解析,重要的几个包:io,session,builder,mapper(annotations,bindi