【问题标题】:FluentMigrator two SQL DatabasesFluentMigrator 两个 SQL 数据库
【发布时间】: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


    【解决方案1】:

    嗯,有时候它比你想象的要容易。对于所有有相同问题的人:

    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();
            }
        }
    

    而在迁移文件中你只需要:

    [Tags("Database1")]
    [Migration(1)]
    public class FooScript : Migration
    {
    }
    

    更多信息在这里: https://fluentmigrator.github.io/articles/migration/migration-filter-tags.html?tabs=runner-internal

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

    【讨论】:

    • 您能否详细说明“将 FluentMigration 添加到您的 ServiceCollection”是什么意思?至于您的解决方案,我认为这很好,但我实际上会推荐两个单独的 csproj,每个都包含自己的迁移,因为 (a) 连接字符串不同 (b) 数据库名称不同 (c) 表不同 (d) 数据不同。从您的原始问题中,我实际上没有看到任何共同点,这让我认为在这里使用标签是个好主意。以后调试会困难很多,因为您忘记在迁移中包含标签。
    猜你喜欢
    • 1970-01-01
    • 2018-10-25
    • 2016-02-25
    • 2011-05-29
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多