-
Notifications
You must be signed in to change notification settings - Fork 5
Running Migrations
Chase Florell edited this page Jul 12, 2015
·
4 revisions
Running your migrations are quite easy. Just get all of your migration out of your IoC container (in order).
var migrations = _container.Resolve<IEnumerable<IMigration>>()
.OrderBy(x => typeof (IMigration).Name)
.ToList();
Create a new MigrationRunner, passing in the DbProvider being used.
var migrationRunner = new MigrationRunner(_container.Resolve<IDbProvider>());
And then simply RunAll
migrationRunner.RunAll(SystemRole.Client, migrations);
Here's a complete example of how we register the DbProvider
note: Registering your Migrations is straight forward, just register them the way you register everything else, and do it in the PCL
Platform Specific - IDbProvider Registration
SQLite
// Inside the the platform specific code
// AppDelegate (iOS)
// MainActivity (Android)
containerBuilder.RegisterType<DbProvider>()
.SingleInstance()
.WithParameters(new List<Parameter> {
new TypedParameter(typeof(string), "database.sqlite3"),
new TypedParameter(typeof(SqliteSettings), new SqliteSettings {
CacheSize = 16777216,
SyncMode = SynchronizationModes.Off,
JournalMode = SQLiteJournalModeEnum.Off,
PageSize = 65536
})
})
.As<IDbProvider>();
SQL Server
var dbConnection = ConfigurationManager.ConnectionStrings["databaseConnection"];
var databaseName = ConfigurationManager.AppSettings["databaseName"];
var connectionProvider = new DbConnectionProvider(dbConnection.ConnectionString, dbConnection.ProviderName);
var dbProvider = new DbProvider(connectionProvider, databaseName);
containerBuilder.RegisterInstance(dbProvider).As<IDbProvider>().SingleInstance();
Portable Class Library - Run Migrations
private static void RunMigrations()
{
var migrations = _container.Resolve<IEnumerable<IMigration>>()
.OrderBy(x => typeof (IMigration).Name)
.ToList();
var migrationRunner = new MigrationRunner(_container.Resolve<IDbProvider>());
migrationRunner.RunAll(SystemRole.Client, migrations);
}
note: these examples are shown using Autofac as the IoC container. You're free to use whatever IoC Container you like.