【问题标题】:How do I run a profile migration using FluentMigrator?如何使用 FluentMigrator 运行配置文件迁移?
【发布时间】:2013-06-14 00:24:16
【问题描述】:

我正在使用 FluentMigrator 管理我的数据库更改,我像这样执行我的迁移:

const string connectionString = @"Data Source=localhost, 1433;Initial Catalog=testdb;Integrated Security=SSPI;";
            Announcer announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
            announcer.ShowSql = true;

            Assembly assembly = Assembly.GetAssembly(typeof (MigrationMarker));
            IRunnerContext migrationContext = new RunnerContext(announcer);

            var options = new ProcessorOptions
                              {
                                  PreviewOnly = false, // set to true to see the SQL
                                  Timeout = 60
                              };

            var factory = new SqlServer2008ProcessorFactory();
            IMigrationProcessor processor = factory.Create(connectionString, announcer, options);
            var runner = new MigrationRunner(assembly, migrationContext, processor);

            runner.MigrateUp(true);

但我不知道如何为特定配置文件执行迁移?

所以鉴于我的迁移器具有这样的属性:

[Profile("DevMigration")]
public class DevMigration : FluentMigrator.Migration
{

我尝试了以下几种变体:

runner.ProfileLoader.FindProfilesIn(assembly, "DevMigrator");
runner.ApplyProfiles();

但我还没有走近,有人知道我如何使用运行器执行配置文件迁移吗?

【问题讨论】:

    标签: c# fluent-migrator


    【解决方案1】:

    尝试在迁移上下文中设置配置文件,然后将其传递给迁移运行器,如下所示:

    IRunnerContext migrationContext = new RunnerContext(announcer);
    migrationContext.Profile = "DevMigrator"
    

    配置文件加载器方法FindProfilesIn 仅返回带有配置文件的迁移。 RunnerContext 的构造函数加载 ProfileLoader,它默认加载上下文中指定配置文件的迁移(我认为这默认为 null,因此没有配置文件迁移)。

    您不需要手动调用ApplyProfiles 方法,因为这是在MigrateUp(bool) 方法中调用的。

    【讨论】:

      【解决方案2】:

      供人们稍后阅读并使用进程内迁移器,如in the docs here 所示。在这种情况下指定配置文件名称的方法是在 ServiceCollection 上添加另一个配置调用,如下所示:

                  .Configure<RunnerOptions>(cfg =>
                  {
                      cfg.Profile = "DevMigration";
                  })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-01
        • 1970-01-01
        • 2016-11-24
        • 2020-04-19
        • 2015-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多