4

In my current web project I'm trying to set up code migrations. I set up my db initialiser as follows in my MVC4 project

protected void Application_Start()
{
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
}

I can make changes to my code first models and this updates the database to the latest automatic migration as expected when I start up the website.

However I'm trying to add an explicit migration through the console Add-Migration FirstMigration so I can add some indexes. This adds the code file 201301071708126_FirstMigration to my project and I can add in my index code here easily enough.

But this won't run automatically when I restart the website. I have to run Update-Database from the console to apply this migration.

I followed this tutorial on msdn but I can't see what I'm doing wrong as it suggests to me that the explicit migration should run automatically.

In my configuration file I have the following constructor

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}
4

2 回答 2

4

不久前我找到了答案,但我错过了问题中的一些信息。

我有 2 个网站,一个 MVC 应用程序和一个 WEBAPI 应用程序。我需要在两者中都进行数据库初始化,否则它不能正常工作。

于 2013-02-05T15:59:49.337 回答
-1

在我的应用程序构造函数中,我有

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MYContext>());

它会更新模型。

于 2013-02-05T15:22:38.333 回答