简体   繁体   English

如何使用FluentMigrator运行配置文件迁移?

[英]How do I run a profile migration using FluentMigrator?

I am using FluentMigrator to manage my database changes, I execute my migrations like this: 我使用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);

What I can't figure out though, is how to execute a migration for a specific profile? 我无法弄清楚的是,如何为特定的配置文件执行迁移?

So given that my migrator has an attribute like this: 因此,鉴于我的迁移器具有如下属性:

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

I have tried a few variations of: 我尝试了几种变体:

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

But I'm not getting any closer, does anyone know how I can execute a profile migration using the runner? 但是我没有接近,有没有人知道如何使用跑步者执行配置文件迁移?

Try setting the profiles on the migration context before being passed to the migration runner like this: 尝试在迁移上下文中设置配置文件,然后将其传递给迁移运行器,如下所示:

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

The profile loader method FindProfilesIn only returns the migrations with the profile. 概要文件加载器方法FindProfilesIn仅返回具有概要文件的迁移。 The constructor of the RunnerContext loads the ProfileLoader which by default loads the migrations for the specified profile in the context (I think this defaults to null therefore having no profile migrations). RunnerContext的构造RunnerContext加载ProfileLoader ,默认情况下会在上下文中加载指定配置文件的迁移(我认为这默认为null,因此没有配置文件迁移)。

You shouldn't need to manually call the ApplyProfiles method as this is called in the MigrateUp(bool) method. 您不需要手动调用ApplyProfiles方法,因为在MigrateUp(bool)方法中调用了该方法。

For the people reading it much later and using in-process migrator as it's shown in the docs here . 对于那些阅读它并且使用进程内迁移器的人来说,就像在这里的文档中所示。 The way to specify Profile name in this case is adding another Configure call on ServiceCollection like this: 在这种情况下指定配置文件名称的方法是在ServiceCollection上添加另一个Configure调用,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM