EF Code First DbMigration without nuget

风流意气都作罢 提交于 2019-11-29 01:47:52

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)

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.

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.

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.

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

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!