Is it possible to use a smooth migrator in application_start?

I use a free migrator to manage my migrations in the database, but what I would like to do is the migration that starts when the application starts. The closest I managed:

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 am sure that this worked for me, but I have never considered it (a hobby project), and now it throws null reference exceptions when it gets into the Execute line. Unfortunately, there are no documents for this, and for a long time I hit my head about it.

Has anyone been able to get this kind of work with FluentMigrator?

+46
c # asp.net-mvc-3 fluent-migrator
Sep 27 '11 at 19:07
source share
2 answers

PM> Install-Package FluentMigrator.Tools

In manual mode, add a link to:

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

Note that the folder name will be different from the version number, in this illustration the current version 1.6.1 is used. If you need a .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); } } } 

Please note: SqlServer2008ProcessorFactory is customizable depending on your database, there is support for: 2000, 2005, 2008, 2012 and 2014.

+61
May 09 '12 at
source share

I really did the running migrations in application_start, however, from this code it's hard to say what might be wrong ... Since this is open source, I would just grab the code and pulled it into your solution and built it to find what the Execute method complains from . I found that the source code for Fluent Migrator is pretty well organized.

One thing you might have to worry about if this is a web application is that no one uses the database during the migration. I used the strategy of establishing a connection, setting up the database in single-user mode, migrating, setting up the database in multi-user mode, and then closing the connection. It also handles the load-balanced web application script on multiple servers, so 2 servers do not try to start migrations from the same database.

+5
Sep 30 '11 at 15:58
source share



All Articles