使用 fluentMigrator 时如何 运行 只迁移特定的数据库名称
How to run migration on only specific database name when using fluentMigrator
我们正在使用 .NET4.5
、SQL2012
和 FluentMigrator
to control our database migrations。我们在我们的解决方案中 运行 连接了多个数据库,我们需要 运行 将一些数据插入到一些数据库而不是其他数据库中。
如何运行 根据特定数据库名称迁移某些数据库?
我已经介绍了这个 class 来控制它应该 运行 在哪些数据库上。因此,从 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"};
}
}
}
我们正在使用 .NET4.5
、SQL2012
和 FluentMigrator
to control our database migrations。我们在我们的解决方案中 运行 连接了多个数据库,我们需要 运行 将一些数据插入到一些数据库而不是其他数据库中。
如何运行 根据特定数据库名称迁移某些数据库?
我已经介绍了这个 class 来控制它应该 运行 在哪些数据库上。因此,从 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"};
}
}
}