Is it possible to use fluent migrator in application_start?

I'm using fluent migrator to manage my database migrations, but what I'd like to do is have the migrations run at app start. The closest I have managed is this:

public static void MigrateToLatest(string connectionString)
{
    using (var announcer = new TextWriterAnnouncer(Console.Out)
                                {
                                    ShowElapsedTime = true,
                                    ShowSql = true
                                })
    {
        var assembly = typeof(Runner).Assembly.GetName().Name;

        var migrationContext = new RunnerContext(announcer)
        {
            Connection = connectionString,
            Database = "SqlServer2008",
            Target = assembly
        };

        var executor = new TaskExecutor(migrationContext);
        executor.Execute();
    }
}

I'm sure I had this working, but I've not looked at it for sometime (hobby project) and it's now throwing null reference exceptions when it gets to the Execute line. Sadly there are no docs for this and I've been banging my head on it for ages.

Has anyone managed to get this kind of thing working with FluentMigrator?


PM> Install-Package FluentMigrator.Tools

Manually add a reference to:

packages\FluentMigrator.Tools.1.6.1\tools\AnyCPU\40\FluentMigrator.Runner.dll

Note that the folder name will vary on version number, this illustration uses the current 1.6.1 release. If you need the .NET 3.5 runner use the \35\ directory.

public static class Runner
{
    public class MigrationOptions : IMigrationProcessorOptions
    {
        public bool PreviewOnly { get; set; }
        public string ProviderSwitches { get; set; }
        public int Timeout { get; set; }
    }

    public static void MigrateToLatest(string connectionString)
    {
        // var announcer = new NullAnnouncer();
        var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
        var assembly = Assembly.GetExecutingAssembly();

        var migrationContext = new RunnerContext(announcer)
        {
            Namespace = "MyApp.Sql.Migrations"
        };

        var options = new MigrationOptions { PreviewOnly=false, Timeout=60 };
        var factory = 
            new FluentMigrator.Runner.Processors.SqlServer.SqlServer2008ProcessorFactory();

        using (var processor = factory.Create(connectionString, announcer, options))
        { 
            var runner = new MigrationRunner(assembly, migrationContext, processor);
            runner.MigrateUp(true);
        }
    }
}

Note the SqlServer2008ProcessorFactory this is configurable dependent upon your database, there is support for: 2000, 2005, 2008, 2012, and 2014.


I have actually accomplished running migrations in the application_start however it is hard to tell from that code what could be wrong... Since it is open source I would just grab the code and pull it into your solution and build it to find out what the Execute method is complaining about. I found that the source code for Fluent Migrator is organized pretty well.

One thing that you might have to be concerned about if this is a web app is making sure that no one uses the database while you are migrating. I used a strategy of establishing a connection, setting the database to single user mode, running the migrations, setting the database to multi user mode, then closing the connection. This also handles the scenario of a load balanced web application on multiple servers so 2 servers don't try to run migrations against the same database.