如何在使用fluentMigrator时仅在特定数据库名称上运行迁移

我们使用.NET4.5SQL2012FluentMigrator来控制我们的数据库迁移。 我们在解决方案中运行多个数据库,我们需要将一些数据插入到一些数据库中,而不是其他数据库中。

我如何根据特定的数据库名称运行一些数据库迁移?


我已经引入了这个类来控制它应该运行的数据库。 所以,当从Migration继承时,您现在将从OnlyRunOnSpecificDatabaseMigration继承:

一个注意!:如果DatabaseNamesToRunMigrationOnList中没有列出任何数据库,它会回退到默认行为(运行迁移) - 有些可能会发现违反直觉

namespace Infrastructure.Migrations
{
    using System.Collections.Generic;
    using FluentMigrator;
    using FluentMigrator.Infrastructure;

    public abstract class OnlyRunOnSpecificDatabaseMigration : Migration
    {
        public abstract List<string> DatabaseNamesToRunMigrationOnList { get; }

        private bool DoRunMigraton(IMigrationContext context)
        {
            return this.DatabaseNamesToRunMigrationOnList == null ||
                   this.DatabaseNamesToRunMigrationOnList.Contains(new System.Data.SqlClient.SqlConnectionStringBuilder(context.Connection).InitialCatalog);
        }

        public override void GetUpExpressions(IMigrationContext context)
        {
            if (this.DoRunMigraton(context))
            {
                base.GetUpExpressions(context);
            }
        }

        public override void GetDownExpressions(IMigrationContext context)
        {
            if (this.DoRunMigraton(context))
            {
                base.GetDownExpressions(context);
            }
        }
    }
}

用法示例:

public class RiskItems : OnlyRunOnSpecificDatabaseMigration
{
    public override void Up()
    {

        Execute.Sql(@"update [Items] set  
                    CanBeX = 
                    case when exists(select 1 from [SomeTable] where Key = [Items].Key and position like 'Factor%') then 1 else 0 end");
    }

    public override void Down()
    {

    }

    public override List<string> DatabaseNamesToRunMigrationOnList
    {
        get
        {
            return new List<string> {"my_database_name"};
        }
    }
}
链接地址: http://www.djcxy.com/p/62331.html

上一篇: How to run migration on only specific database name when using fluentMigrator

下一篇: can dacpac be used for managing databases having large volume of data?