【发布时间】:2019-09-26 15:11:33
【问题描述】:
我使用两个数据库并希望使用 fluent 迁移器。它们是两个 SQL 数据库,它们可以位于不同的服务器上并且具有不同的连接字符串。他们有不同的表格和数据。但我只有一个初创公司和一个项目。我想在每次迁移时指定使用的数据库。我该如何管理?我目前正在尝试用属性解决它,但我不确定它是否能以某种方式工作:
public class MyAttribute : Attribute
{
public MyAttribute(string name)
{
Name = name;
}
public string Name;
}
[MyAttribute("Database1")]
[Migration(1)]
public class FooScript : Migration
{
public override void Down()
{
Delete.Table("Foo");
}
public override void Up()
{
Create.Table("Foo")
.WithColumn("id").AsInt16().PrimaryKey()
.WithColumn("Body").AsString(4000).NotNullable();
}
}
在启动时:
services.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
.AddSqlServer()
.WithGlobalConnectionString("")
.ScanIn(typeof(database1).Assembly, typeof(database1).Assembly).For.Migrations())
.Configure<ProcessorOptions>(x =>
{
var connection = typeof(FooScript).GetCustomAttributes(typeof(MyAttribute), true);
var name = "0"; //here I would need the current executing Migration class
switch (name)
{
case "Database1":
x.ConnectionString = Configuration.GetConnectionString("Database1");
break;
case "Database1":
x.ConnectionString = Configuration.GetConnectionString("Database1");
break;
}
})
.BuildServiceProvider();
但它不起作用。我需要当前脚本的属性来覆盖数据库。但是 proucessorOptions 似乎没有这些。是否有其他解决方案或其他配置可以覆盖?还有其他想法如何使用两个或多个不同的 sql 数据库运行 fluentmigration?
理论上,我最终会得到每个数据库的 VersionInfo 以及这些数据库的当前迁移。希望这有效。
【问题讨论】:
标签: .net-core connection-string fluent-migrator