项目

单层解决方案:数据库配置

本文档中提及的某些功能可能在免费版本中不可用。我们使用 * 符号表示该功能仅在 团队版更高级别 许可证中提供。

ABP Studio的单层解决方案模板包含了预配置的数据库设置。本文档解释如何在您的解决方案中管理数据库配置。

连接字符串

连接字符串存储在 appsettings.json 文件中,并且可以通过编辑此文件为不同的环境进行自定义。Web应用程序项目默认使用名为 Default 的连接字符串。

要更新 Default 键对应的连接字符串,请修改项目中的 appsettings.json 文件。连接字符串在 ConnectionStrings 节点下定义,如下所示:

{
  "ConnectionStrings": {
    "Default": "Server=(LocalDb)\\MSSQLLocalDB;Database=Bookstore;Trusted_Connection=True;TrustServerCertificate=true"
  }
}

DbContext 类

在单层解决方案模板中,DbContext 类在主项目中定义。此类管理数据库架构,并派生自 AbpDbContext 类,后者提供了额外的功能和配置。您可以自定义 DbContext 类以添加新的实体、关系和配置。它位于主项目的 Data 文件夹中。

public class BookstoreDbContext : AbpDbContext<BookstoreDbContext>
{
    
    public const string DbTablePrefix = "App";
    public const string DbSchema = null;

    public BookstoreDbContext(DbContextOptions<BookstoreDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* 将模块包含到您的迁移数据库上下文中 */

        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureFeatureManagement();
        builder.ConfigurePermissionManagement();
        builder.ConfigureBlobStoring();
        builder.ConfigureIdentityPro();
        builder.ConfigureOpenIddictPro();
        builder.ConfigureGdpr();
        builder.ConfigureLanguageManagement();
        builder.ConfigureSaas();
        builder.ConfigureTextTemplateManagement();
        
        /* 在此处配置您自己的实体 */
    }
}

OnModelCreating 方法

OnModelCreating 方法用于配置数据库架构。它调用 ABP 框架的 Configure* 方法来为各模块配置数据库架构。您也可以在此方法内部配置您自己的表/实体。

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.ConfigurePermissionManagement();
    builder.ConfigureSettingManagement();
    builder.ConfigureBackgroundJobs();
    builder.ConfigureAuditLogging();
    builder.ConfigureFeatureManagement();
    builder.ConfigureIdentityPro();
    builder.ConfigureOpenIddictPro();
    builder.ConfigureLanguageManagement();
    builder.ConfigureSaas();
    builder.ConfigureTextTemplateManagement();
    builder.ConfigureGdpr();
    builder.ConfigureCmsKit();
    builder.ConfigureCmsKitPro();
    builder.ConfigureBlobStoring();
    
    /* 在此处配置您自己的表/实体 */

    //builder.Entity<YourEntity>(b =>
    //{
    //    b.ToTable(DbTablePrefix + "YourEntities", DbSchema);
    //    b.ConfigureByConvention(); // 为基类属性自动配置
    //    //...
    //});
}

Configure* 方法是在每个模块的 EntityFrameworkCore 项目中定义的扩展方法。这些方法用于为其各自的模块配置数据库架构。

配置

BookstoreModule 类中,ConfigureEfCore 方法用于配置数据库上下文。它将 BookstoreDbContext 类注册到依赖注入系统中,并将 SQL Server 设置为应用程序的默认数据库管理系统。

private void ConfigureEfCore(ServiceConfigurationContext context)
{
    context.Services.AddAbpDbContext<BookstoreDbContext>(options =>
    {
        /* 您可以移除 "includeAllEntities: true" 来仅为聚合根创建默认仓储
            * 文档:https://docs.abp.io/en/abp/latest/Entity-Framework-Core#add-default-repositories
            */
        options.AddDefaultRepositories(includeAllEntities: true);
    });

    Configure<AbpDbContextOptions>(options =>
    {
        options.Configure(configurationContext =>
        {
            configurationContext.UseSqlServer();
        });
    });
    
}

IDesignTimeDbContextFactory 的实现

IDesignTimeDbContextFactory 接口用于在设计时创建 DbContext 实例。EF Core 工具使用它来创建迁移和更新数据库。BookstoreDbContextFactory 类实现了 IDesignTimeDbContextFactory 接口来创建 BookstoreMigrationsDbContext 实例。

public class BookstoreDbContextFactory : IDesignTimeDbContextFactory<BookstoreDbContext>
{
    public BookstoreDbContext CreateDbContext(string[] args)
    {
        BookstoreEfCoreEntityExtensionMappings.Configure();
        var configuration = BuildConfiguration();

        var builder = new DbContextOptionsBuilder<BookstoreDbContext>()
            .UseSqlServer(configuration.GetConnectionString("Default"));

        return new BookstoreDbContext(builder.Options);
    }

    private static IConfigurationRoot BuildConfiguration()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false);

        return builder.Build();
    }
}

SaaS 模块:租户管理UI *

SaaS 模块提供了必要的UI来为租户设置和更改连接字符串,并触发数据库迁移。

连接字符串管理模态框

您可以在SaaS模块的租户页面上,点击某个租户操作下拉按钮中的数据库连接字符串命令:

数据库连接字符串

这会打开数据库连接字符串模态框,如下所示:

数据库连接字符串模态框

在这里,我们可以为该租户设置一个默认连接字符串

当您进行更改并保存对话框后,数据库会自动创建并迁移。如果您后续更新连接字符串(例如更改数据库名称),它也会再次触发数据库迁移过程。

手动应用数据库迁移

如果您需要为特定租户手动触发数据库迁移,请在SaaS模块的租户管理页面上,点击相关租户的操作下拉菜单,并选择应用数据库迁移命令:

应用数据库迁移


在本文档中