12

How to migrate DB without nuget? It is not possible to use Visual Studio with nuget in production environment. Currently, many examples only teach us to use Visual Studio with nuget. How to use the generated DbMigration classes?

6 Answers 6

12

The easiest way is:

Database.SetInitializer(
    new MigrateDatabaseToLatestVersion<MyDbContext,
                                       MyDbMigrationsConfiguration>());

This will run the migrations when initializing the DbContext.

You can also force the execution manually:

var migrator = new DbMigrator(new MyMigrationsConfiguration());
migrator.Update();

(I believe you also have to set TargetDatabase on the configuration, but you can try)

5

Here are the options:

  1. Use the migrate.exe command line tool that ships in our NuGet package.
  2. Use the MigrateDatabaseToLatestVersion initializer as others have described.
  3. Use the runtime API available from the DbMigrator class.
4

You can migrate to the latest version using a Web.config setting - see this blog post by Rowan Miller:

If you are using Code First Migrations, you can configure the database to be migrated automatically using the MigrateDatabaseToLatestVersion initializer.

<contexts>
  <context type="Blogging.BlogContext, MyAssembly">
    <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[Blogging.BlogContext, 
MyAssembly], [Blogging.Migrations.Configuration, MyAssembly]], EntityFramework" />
  </context>
</contexts>

Just swap your context class in here: the System.Data.Entity.MigrateDatabaseToLatestVersion is built-in to EF. This setting updates the old AppSettings version of the same idea.

To my mind this is the best way, because the question of which initializer to use is a configuration one really, and you want to be able to Web.config this, and ideally apply config transforms to work for your different environments.

2

You can do it using the EF Power Tools, there's the migrate.exe program that you can use to run migrations from the command prompt (post build for example). If you want to run migrations on production database you can also use the Update-Database command to generate SQL scripts from the migration classes, very useful if you need to pass through a DBA.

EF Power Tools are available on the Visual Studio Gallery and optionally here, check out this very useful video that, among other things, talks about the Update-Database command.

1

there is another solution :

 Using DB = New SHAContext()
        If DB.Database.Exists() Then
            Dim migrator As New DbMigrator(New SHAClassLibrary.Migrations.Configuration())
            For Each m In migrator.GetDatabaseMigrations()
                Try
                    migrator.Update(m)
                Catch ex As Exception

                End Try
            Next
        End If
        'DB.test()
    End Using
0

I was looking for a way to control which migrations run explicitly in code without the need of a DbConfiguration class or automatic migrations enabled.

So i managed to create the following extension:

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);
    }
}

As an example, having a migration like the following:

public class CreateIndexOnContactCodeMigration : DbMigration
{
    public override void Up()
    {
        this.CreateIndex("Contacts", "Code");
    }

    public override void Down()
    {
        base.Down();
        this.DropIndex("Contacts", "Code");
    }
}

You could run it using your DbContext:

using (var dbCrm = new CrmDbContext(connectionString))
{
    var migration = new CreateIndexOnContactCodeMigration();
    migration.Up();                
    dbCrm.RunMigration(migration);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.