I would like to control which migrations are performed explicitly in the code, and after a lot of searching, I managed to develop the following method without the need to include the DbConfiguration class or automatic migrations:
public static void RunMigration(this DbContext context, DbMigration migration) { var prop = migration.GetType().GetProperty("Operations", BindingFlags.NonPublic | BindingFlags.Instance); if (prop != null) { IEnumerable<MigrationOperation> operations = prop.GetValue(migration) as IEnumerable<MigrationOperation>; var generator = new SqlServerMigrationSqlGenerator(); var statements = generator.Generate(operations, "2008"); foreach (MigrationStatement item in statements) context.Database.ExecuteSqlCommand(item.Sql); } }
And if we had such a migration:
public class CreateIndexOnContactCodeMigration : DbMigration { public override void Up() { this.CreateIndex("Contacts", "Code"); } public override void Down() { base.Down(); this.DropIndex("Contacts", "Code"); } }
We will use it like this:
using (var dbCrm = new CrmDbContext(connectionString)) { var migration = new CreateIndexOnContactCodeMigration(); migration.Up(); dbCrm.RunMigration(migration); }
Sincerely.
Panos Roditakis Jan 13 '16 at 13:02 2016-01-13 13:02
source share