太長不看版:
對於內置表,在所有啟動專案(不包括
XXX.DbMigrator)的Program.cs文件和XXXDbContextFactory中,設置AbpCommonDbProperties和AbpOpenIddictDbProperties的值。如果是我們自己的實體表,那麼找到 XXX.Domain/XXXConsts.cs 文件,修改相關值即可。
Abp 框架本身包含一些表,例如 AbpAuditLogs、AbpAuditLogActions 表等,其中 Abp 就是表名前綴,這個值支持修改。
修改 Abp 內置表的表名前綴和 Schema#
生成模板專案#
我們先使用 Cli 來生成模板專案
// abp version 6.0.2
abp new MyApp
打開生成的專案,找到 MyApp.EntityFrameworkCore 專案。
打開 Migrations/MyAppDbContextModelSnapshot.cs 文件,大概在 144 行,可以看到一句
b.ToTable("AbpAuditLogs", (string)null);
可以看到,默認生成的模板專案中,使用 Abp 作為表前綴,而 schema 則設置為 null。
我們將這個 Migrations 文件夾刪除。
找到設置表名前綴的地方#
還是 MyApp.EntityFrameworkCore 專案,我們找到 EntityFrameworkCore/MyAppDbContext.cs 文件。
public class MyAppDbContext :
    AbpDbContext<MyAppDbContext>,
    IIdentityDbContext,
    ITenantManagementDbContext
{
    // ...
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // 這裡設置了一些內置表的配置
        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureFeatureManagement();
        builder.ConfigureTenantManagement();
        // ...
    }
}
找到 OnModelCreating 方法,我們可以進入這些 builder.ConfigureXXX() 方法,看看源碼實現。
按住 Ctrl 鍵,再鼠標左擊,可以反編譯,看到 Abp 的對應源碼。我們點開 ConfigurePermissionManagement 方法,可以看到以下代碼
public static void ConfigurePermissionManagement([NotNull] this ModelBuilder builder)
{
    // ...
    builder.Entity<PermissionGrant>(b =>
    {
        // 這裡設置了表名前綴,以及 Schema
        b.ToTable(AbpPermissionManagementDbProperties.DbTablePrefix + "PermissionGrants", AbpPermissionManagementDbProperties.DbSchema);
        // ...
    });
    // ...
}
我們可以看到 PermissionGrant 實體對應的表名配置,和 AbpPermissionManagementDbProperties 掛鉤。點開其他 builder.ConfigureXXX() 方法,也能看到類似的配置。
那我們繼續再找到 AbpPermissionManagementDbProperties 類,繼續探索。
public static class AbpPermissionManagementDbProperties
{
    public static string DbTablePrefix { get; set; } = AbpCommonDbProperties.DbTablePrefix;
    public static string DbSchema { get; set; } = AbpCommonDbProperties.DbSchema;
    public const string ConnectionStringName = "AbpPermissionManagement";
}
可以看到有關 PermissionGrant 實體的所有配置,包括表名前綴 DbTablePrefix、Schema DbSchema 和數據庫鏈接字符串名 ConnectionStringName。也就是說,我們可以在專案啟動的時候,單獨修改這些值,就能修改相關的配置。
AbpPermissionManagementDbProperties 類中的表名前綴 DbTablePrefix 和 Schema DbSchema 都依賴於 AbpCommonDbProperties 類,我們繼續找到這個類的實現。
public static class AbpCommonDbProperties
{
    /// <summary>
    /// This table prefix is shared by most of the ABP modules.
    /// You can change it to set table prefix for all modules using this.
    ///
    /// Default value: "Abp".
    /// </summary>
    public static string DbTablePrefix { get; set; } = "Abp";
    /// <summary>
    /// Default value: null.
    /// </summary>
    public static string DbSchema { get; set; } = null;
}
可以看到,這個配置項,就和默認模板生成的配置一致,表名前綴以 Abp 開頭,Schema 設置為 null。找到了基礎設置項,我們就可以修改默認的配置。
修改默認配置#
在所有的啟動專案(不包括 XXX.DbMigrator)中的 Program.cs 文件中,以及 XXXDbContextFactory.cs 文件中,設置配置。
// 所有的啟動專案(不包括 `XXX.DbMigrator`)
public class Program
{
    public async static Task<int> Main(string[] args)
    {
        AbpCommonDbProperties.DbSchema      = "abp";
        AbpCommonDbProperties.DbTablePrefix = "ABP_";
        // ...
    }
}
// XXXDbContextFactory.cs (默認生成在 XXX.EntityFrameworkCore/EntityFrameworkCore)
public XXXDbContext CreateDbContext(string[] args)
{
    AbpCommonDbProperties.DbSchema      = "abp";
    AbpCommonDbProperties.DbTablePrefix = "ABP_";
    // ...
}
生成首次遷移代碼#
我們需要先刪除 XXX.EntityFrameworkCore 專案下的 Migrations 文件夾,這裡面是原先默認生成的遷移文件。
然後再運行 XXX.DbMigrator 專案,生成遷移文件。
這次我們再打開 Migrations/MyAppDbContextModelSnapshot.cs 文件,找到 144 行,可以看到生成的遷移代碼中已經應用了我們的配置。
// 表名前綴 ABP_
// Schema abp
b.ToTable("ABP_AuditLogs", "abp");
特殊的表#
運行遷移後,我們來到數據庫查看,會發現有四張表的表名前綴和 Schema,並沒有按我們的預期設置,這幾張表以 OpenIddict 為前綴。
我們回到代碼中,找到 MyAppDbContext.cs 文件的 OnModelCreating 方法,可以找到這行代碼。
builder.ConfigureOpenIddict();
我們重新朔源,反編譯查看源碼可以看到,以 OpenIddict 為前綴的幾個實體類,表配置和 AbpOpenIddictDbProperties 類掛鉤,而 AbpOpenIddictDbProperties 類裡的配置並沒有依賴於 AbpCommonDbProperties,所以我們需要單獨設置這幾個實體。
// 在前面的 Program.cs 和 XXXDbContextFactory.cs 中,添加以下配置
AbpOpenIddictDbProperties.DbSchema      = "abp";
AbpOpenIddictDbProperties.DbTablePrefix = "ABP_";
重新刪除 Migrations 文件夾,清空數據庫,然後運行 XXX.DbMigrator 專案,應用遷移後,就可以在數據庫看到生成的表了。
單獨配置#
從 MyAppDbContext.cs 文件的 OnModelCreating 方法中的以下代碼中,我們可以找到一些相應配置,可以反編譯源碼,找到每個配置所依賴的 XXXDbProperties,在 Program.cs 和 XXXDbContextFactory.cs 文件中,添加相應配置的設置即可。
builder.ConfigurePermissionManagement();
builder.ConfigureSettingManagement();
builder.ConfigureBackgroundJobs();
builder.ConfigureAuditLogging();
builder.ConfigureIdentity();
builder.ConfigureOpenIddict();
builder.ConfigureFeatureManagement();
builder.ConfigureTenantManagement();
用戶表的表名前綴和 Schema#
用戶表的這些配置比較簡單,直接就在生成的代碼中可以找到。
// MyApp.Domain 專案
// MyAppConsts 類
public static class MyAppConsts
{
    public const string DbTablePrefix = "App";
    public const string DbSchema = null;
}
修改這些配置就可以了。