我目前正在将 .net 应用程序迁移到 PostgreSQL 15。因此,我需要将所有表从
public
模式移至自定义模式。
我的
__EFMigrationHistory
表也需要移至自定义架构。
我找到了以下控制方法:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection"),
x => x.MigrationsHistoryTable("__MyMigrationsHistory", "not-public")));
但是,我们从
UseNpgsql()
文件配置 appsettings.json
。大致如下:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
private IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(options =>
{
options.UseNpgsql(Configuration);
});
}
虽然我们的
appsettings.json
看起来如下:
"Postgres": {
"Client": {
"Server": "localhost",
"Port": "5433",
"Database": "mydatabase",
"Username": "xxx",
"Password": "xxx",
"Include Error Detail": true
}
},
是否可以在
appsettings.json
文件中进行配置?如果是的话,怎么办?
解决这个问题的方法其实很简单。
存在重载,允许将 IConfiguration 与选项回调结合使用。
options.UseNpgsql(myConfiguration, o => o.MigrationsHistoryTable("__EFMigrationHistory", "myCustomSchema"));