zoukankan      html  css  js  c++  java
  • 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开发中有什么遇到的问题或者有什么建议,欢迎大家留言。

  • 相关阅读:
    如何安装ArchLinux
    状态模式
    iOS设备的越狱方法
    浅析Windows安全相关的一些概念
    项目做成jar包
    JavaScript包装对象
    node.js系列笔记之node.js初识《一》
    使用Reactive Extensions(Rx),对短时间内多次发生的事件限流
    in和exists哪个效率高本人测试证明
    Asp.net MVC使用Filter解除Session, Cookie等依赖
  • 原文地址:https://www.cnblogs.com/yanglang/p/9790110.html
Copyright © 2011-2022 走看看