Entity Framework Migrations - Enable AutoMigrations along with added migration

后端 未结 6 1585
青春惊慌失措
青春惊慌失措 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 09:50

    Here is my current solution, which I'm not completely satisfied with.

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    
        var context = new KCSoccerDataContext();
        var initializeDomain = new CreateDatabaseIfNotExists();
        var initializeMigrations = new MigrateDatabaseToLatestVersion();
    
        initializeDomain.InitializeDatabase(context);
        initializeMigrations.InitializeDatabase(context);
    
    }
    

    I'm actually creating two different initializers. The first, using CreateDatabaseIfNotExists, succcessfully goes through and creates tables based on my Domain objects. The second, using MigrateDatabaseToLatestVersion, executes all of my explicit migrations.

    I don't like it because Automatic Migrations are basically disabled. So in order to add or change my Domain model I have to completely drop the database and recreate it. This won't be acceptable once I've moved the application to production.

提交回复
热议问题