私はしばらくの間、プロジェクト (VS2012 Express の ASP.NET MVC) で Entity Framework (5.0) を使用しています。ただし、現在、移行を追加することはできません。
PM > Add-Migration -projectName MyProject.DAL TestMigration
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
これが手がかりになるかどうかはわかりませんが、「Unable to ...」というテキストが赤で表示されます。
自動移行を有効にしようとしましたが (保留中のモデルの変更をコードベースの移行に書き込もうとしているので意味がありません)、データベースで必要な移行が行われます。ただし、プロジェクトに移行がないため、これは私が望むものではありません。
データベースを削除して、データベースを再作成しようとしました。データベースは (以前の移行まで) 再作成されますが、その後 Add-Migration を使用しようとすると、「更新できません..」というエラーが表示されます。
編集
-force パラメータを試しましたが、違いはありませんでした。
構成クラスの内容 (以前の移行後に何も変更していません):
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(Bekosense.DAL.Context.BekosenseContext context)
{
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageDrop);
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageCreate);
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentDrop);
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentCreate);
context.Database.ExecuteSqlCommand(Properties.Resources.AddDbUsers);
}
編集 2 DbContext で次の行をコメントアウトすると、追加移行を実行できることがわかりました。
//Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
上記の行をアクティブのままにして、構成ファイル内のすべてをコメントアウトしても、まだ機能しません。Database.SetInitializer 行がこの奇妙な動作を引き起こすのはなぜですか?