.net core FluentMigrator两个SQL数据库

.net core FluentMigrator两个SQL数据库,.net-core,connection-string,fluent-migrator,.net Core,Connection String,Fluent Migrator,我使用两个数据库,希望使用fluent migrator。它们是两个SQL数据库,可以位于不同的服务器上,并且具有不同的连接字符串。它们有不同的表和数据。但我只有一家初创公司和一个项目。我想在每次迁移时指定使用的数据库。我该怎么办?我目前正在尝试用属性来解决它,但我不确定它是否能以某种方式工作: public class MyAttribute : Attribute { public MyAttribute(string name) { Name = name;

我使用两个数据库,希望使用fluent migrator。它们是两个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();
services.AddFluentMigratorCore()
.ConfigureRunner(rb=>rb
.AddSqlServer()文件
.WithGlobalConnectionString(“”)
.ScanIn(typeof(database1).Assembly,typeof(database1.Assembly).For.Migrations())
.Configure(x=>
{
var connection=typeof(FooScript).GetCustomAttributes(typeof(MyAttribute),true);
var name=“0”;//这里我需要当前正在执行的迁移类
交换机(名称)
{
案例“数据库1”:
x、 ConnectionString=Configuration.GetConnectionString(“数据库1”);
打破
案例“数据库1”:
x、 ConnectionString=Configuration.GetConnectionString(“数据库1”);
打破
}
})
.BuildServiceProvider();
但它不起作用。我需要当前脚本的属性来覆盖数据库。但proucessorOptions似乎没有这些功能。我是否可以覆盖其他解决方案或其他配置?还有其他想法吗?如何使用两个或更多不同的sql数据库运行fluentmigration


理论上,我最终会为每个数据库创建一个VersionInfo,并为这些数据库创建当前迁移。希望能奏效。

有时候比你想象的要容易。对于每个有相同问题的人:

startup.cs

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
    {
        //...
        Migration();
    }

    private void Migration()
    {
        var serviceProviderDb1 = new ServiceCollection()
        .AddFluentMigratorCore()
        .ConfigureRunner(rb => rb
            .AddSqlServer()
            .WithGlobalConnectionString(Configuration.GetConnectionString("Database1"))
            .ScanIn(typeof(DB1Context).Assembly).For.Migrations())
        .Configure<RunnerOptions>(opt => {
            opt.Tags = new[] { "Database1" };
        })
        .BuildServiceProvider(false);

        using (var scope = serviceProviderDb1.CreateScope())
        {
            var runner = serviceProviderDb1.GetRequiredService<IMigrationRunner>();
            runner.MigrateUp();
        }

        var serviceProviderDb2 = new ServiceCollection()
            .AddFluentMigratorCore()
            .ConfigureRunner(rb => rb
                .AddSqlServer()
                .WithGlobalConnectionString(Configuration.GetConnectionString("Database2"))
                .ScanIn(typeof(DB2Context).Assembly).For.Migrations())
                .Configure<RunnerOptions>(opt => {
                    opt.Tags = new[] { "Database2" };
                })
            .BuildServiceProvider(false);

        using (var scope = serviceProviderDb2.CreateScope())
        {
            var runner = serviceProviderDb2.GetRequiredService<IMigrationRunner>();
            runner.MigrateUp();
        }
    }
更多信息请点击此处:


唯一不好的是,您没有将FluentMigration添加到ServiceCollection中,并且以后不能将其与依赖项注入一起使用。但是,大多数情况下,您只在启动时运行脚本,因此这应该不是问题

您能否详细说明“将FluentMigration添加到您的ServiceCollection”的含义?至于您的解决方案,我认为还可以,但实际上我建议使用两个单独的csproj,每个csproj都包含自己的迁移,因为(a)连接字符串不同(b)数据库名称不同(c)表不同(d)数据不同。从你最初的问题中我看不到任何共同点,这让我觉得在这里使用标签是个好主意。如果您忘记在迁移中包含标记,那么以后调试会困难得多。
[Tags("Database1")]
[Migration(1)]
public class FooScript : Migration
{
}