private IServiceProvider CreateServices(DataStorageTypes dbOptions, string dbConnectionString) { switch (dbOptions) { case DataStorageTypes.SqlServer: return(new ServiceCollection() .AddFluentMigratorCore() .ConfigureRunner(rb => rb .AddSqlServer() .WithGlobalConnectionString(dbConnectionString) .ScanIn(typeof(DbMigrationEngine).Assembly).For.Migrations()) .AddLogging(lb => lb.AddFluentMigratorConsole()) .BuildServiceProvider(false)); case DataStorageTypes.MySQL: return(new ServiceCollection() .AddFluentMigratorCore() .ConfigureRunner(rb => rb .AddMySql5() .WithGlobalConnectionString(dbConnectionString) .ScanIn(typeof(DbMigrationEngine).Assembly).For.Migrations()) .AddLogging(lb => lb.AddFluentMigratorConsole()) .BuildServiceProvider(false)); default: throw new ArgumentOutOfRangeException(nameof(dbOptions), dbOptions, null); } }
public void MigrateDown(DataStorageTypes dbOptions, string connectionStrings, long toVersion) { IServiceProvider serviceProvider = CreateServices(dbOptions, connectionStrings); using (IServiceScope scope = serviceProvider.CreateScope()) { var runner = scope.ServiceProvider.GetRequiredService <IMigrationRunner>(); runner.MigrateDown(toVersion); } }