.net core FluentMigrator:如何为MigrationRunner设置默认架构
我有一个多租户数据库。租户特定的数据存储在它自己的(PostgreSQL)模式中 我希望能够根据需要使用FluentMigrator部署新租户。我已经设置了一个端点的HTTP post将在其中部署模式。但是,默认情况下会将其部署到公共模式。我希望能够指定要部署到的架构 即.net core FluentMigrator:如何为MigrationRunner设置默认架构,.net-core,fluent-migrator,.net Core,Fluent Migrator,我有一个多租户数据库。租户特定的数据存储在它自己的(PostgreSQL)模式中 我希望能够根据需要使用FluentMigrator部署新租户。我已经设置了一个端点的HTTP post将在其中部署模式。但是,默认情况下会将其部署到公共模式。我希望能够指定要部署到的架构 即 公共类租户服务:ITenantService{ 专用只读服务器ViceProvider(提供程序); 公共租户服务(IServiceProvider提供商){ _提供者=提供者; } 创建公共void(字符串tenantNam
公共类租户服务:ITenantService{
专用只读服务器ViceProvider(提供程序);
公共租户服务(IServiceProvider提供商){
_提供者=提供者;
}
创建公共void(字符串tenantName){
使用(var scope=\u provider.CreateScope()){
var migrationRunner=scope.ServiceProvider.GetService();
//TODO:设置默认模式=租户名称
migrationRunner.MigrateUp();
}
}
}
如何设置MigrationRunner的默认架构
编辑:我已经更新了GitHub回购协议,以反映公认的答案。我一直在为同样的问题和同样的环境而挣扎。Net core、FluentMigrator和Postgre使用不同的模式。我一直在使用过时的函数,allready注意到我必须创建自己的:ConventionSet。我能够在下面解决这个问题,首先我必须说:谢谢你的git,这也帮助我解决了我的问题,如下所示: 我做的第一件事是手动创建一个数据库和一个模式,然后使用不同的名称:public。然后我调整了startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConventionSet>(new DefaultConventionSet("tenanta", null));
services.AddFluentMigratorCore();
services.ConfigureRunner(rb => rb
.AddPostgres()
.WithGlobalConnectionString("Server=localhost;Port=5432;User Id=postgres;Password=12345678;CommandTimeout=20;database=nameofdatabase")
.ScanIn(typeof(AddSecuritySchema).Assembly).For.Migrations());
services.AddSingleton<ITenantService, TenantService>();
services.AddControllers();
}
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
AddSingleton(新的DefaultConventionSet(“租户”,null));
services.AddFluentMigratorCore();
services.ConfigureRunner(rb=>rb
.AddPostgres()
.WithGlobalConnectionString(“服务器=本地主机;端口=5432;用户Id=postgres;密码=12345678;命令超时=20;数据库=数据库名称”)
.ScanIn(typeof(AddSecuritySchema.Assembly).For.Migrations());
services.AddSingleton();
services.AddControllers();
}
通过上面的更改,我们添加了一个:newDefaultConventionSet和schemaname,我最终可以在不同的模式上运行迁移,然后再运行public。但我也希望这是在飞行。因此,为了实现这一点(不确定是否正确),我做了以下工作:
public class TenantService: ITenantService {
private readonly IServiceProvider _provider;
public TenantService(IServiceProvider provider) {
_provider = provider;
}
public void Create(string tenantName) {
var serviceProvider = new ServiceCollection()
.AddSingleton<IConventionSet>(new DefaultConventionSet(tenantName, null))
.AddFluentMigratorCore()
.ConfigureRunner(r => r.AddPostgres()
.WithGlobalConnectionString("Server=localhost;Port=5432;User Id=postgres;Password=12345678;CommandTimeout=20;database=databasename")
.WithRunnerConventions(new MigrationRunnerConventions()
{
})
.ScanIn(typeof(AddSecuritySchema).Assembly).For.Migrations()
)
.Configure<RunnerOptions>(opt =>
{
opt.TransactionPerSession = true;
})
.BuildServiceProvider(false);
using (var scope = serviceProvider.CreateScope())
{
var migrationRunner = scope.ServiceProvider.GetService<IMigrationRunner>();
migrationRunner.MigrateUp();
}
}
}
公共类租户服务:ITenantService{
专用只读服务器ViceProvider(提供程序);
公共租户服务(IServiceProvider提供商){
_提供者=提供者;
}
创建公共void(字符串tenantName){
var serviceProvider=newservicecolection()
.AddSingleton(新的DefaultConventionSet(租户名称,null))
.AddFluentMigratorCore()
.ConfigureRunner(r=>r.AddPostgres()
.WithGlobalConnectionString(“服务器=本地主机;端口=5432;用户Id=postgres;密码=12345678;命令超时=20;数据库=数据库名”)
.WithRunnerConventions(新迁移RunnerConventions()
{
})
.ScanIn(typeof(AddSecuritySchema).Assembly).For.Migrations()
)
.Configure(opt=>
{
opt.TransactionPerSession=true;
})
.BuildServiceProvider(假);
使用(var scope=serviceProvider.CreateScope())
{
var migrationRunner=scope.ServiceProvider.GetService();
migrationRunner.MigrateUp();
}
}
}
当然,首先注册ConventionSet是很重要的;)
public class TenantService: ITenantService {
private readonly IServiceProvider _provider;
public TenantService(IServiceProvider provider) {
_provider = provider;
}
public void Create(string tenantName) {
var serviceProvider = new ServiceCollection()
.AddSingleton<IConventionSet>(new DefaultConventionSet(tenantName, null))
.AddFluentMigratorCore()
.ConfigureRunner(r => r.AddPostgres()
.WithGlobalConnectionString("Server=localhost;Port=5432;User Id=postgres;Password=12345678;CommandTimeout=20;database=databasename")
.WithRunnerConventions(new MigrationRunnerConventions()
{
})
.ScanIn(typeof(AddSecuritySchema).Assembly).For.Migrations()
)
.Configure<RunnerOptions>(opt =>
{
opt.TransactionPerSession = true;
})
.BuildServiceProvider(false);
using (var scope = serviceProvider.CreateScope())
{
var migrationRunner = scope.ServiceProvider.GetService<IMigrationRunner>();
migrationRunner.MigrateUp();
}
}
}