Entity Framework Migrations - Enable AutoMigrations along with added migration

后端 未结 6 1574
青春惊慌失措
青春惊慌失措 2021-02-04 09:34

I\'m utilizing Entity Framework 4.3 Migrations in my project. I would like to use Automatic migrations so that when I make modifications to my domain objects and my context cla

6条回答
  •  北海茫月
    2021-02-04 10:10

    You need to pass a configuration that has the AutomaticMigrationsEnabled set to true in the constructor. Something like this should help:

    
    Database.SetInitializer(new MigrateDatabaseToLatestVersion());
    

    with MyConfiguration being something like:

    
    public class MyConfiguration : Core.Migrations.Configuration
    {
        public MyConfiguration { this.AutomaticMigrationsEnabled = true; }
    }
    

    DISCLAIMER: Just hacked this in, so small tweaks might be required to get this to compile

    EDIT:

    Just checked with EF 4.3.1 and the code is like this for the initializer:

    Database.SetInitializer(new MigrateDatabaseToLatestVersion());
    

    and this for the configuration class:

    public class MyConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration
    {
        public MyConfiguration()
        {
            this.AutomaticMigrationsEnabled = true;
        }
    }
    

提交回复
热议问题