![](/img/trans.png)
[英]Code First Update Database Error 'ApplicationDbContext' context has changed
[英]Azure Mobile Service and EF Code First - context has changed error
我有一個由SQL數據庫支持的Azure移動服務。 我已經愉快地部署了數周,並使用EF Code First管理我的數據庫。 現在,我遇到了麻煩,對移動服務的任何請求都會因錯誤而中斷:
自創建數據庫以來,支持“ [您的上下文]”上下文的模型已更改。
事實是,事實並非如此! 我嘗試了以下方法:
1. Re-depoyed the service *several* times
2. Run ‘Add-Migration’ to see if it mystically picks up any new fields/properties
3. Run ‘Update-database’ which runs without any issues
4. Combinations of 2&3 over and over
5. Deleted the Migration History table
6. Deleted ALL tables from my DB and re-run update-database, which again completes without error
7. 6 then 3, which recreates the database
有什么想法可以解決這個問題嗎?
可以手動或自動執行代碼優先遷移。 我希望選擇自動遷移。 您可以將以下代碼添加到App_Start\\Startup.MobileApp.cs
文件中,以啟用自動遷移,如下所示:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<Models.MobileServiceContext, Migrations.Configuration>());
Or
var migrator = new DbMigrator(new Migrations.Configuration());
migrator.Update();
注意:您需要在Migrations\\Configuration.cs
下將AutomaticMigrationsEnabled配置為true,而無需通過Add-Migration
手動將掛起的模型更改添加到基於代碼Add-Migration
或者您可以使用Add-Migration
添加掛起的模型更改而無需將AutomaticMigrationsEnabled
設置為true 。
根據您的問題,建議您更改數據庫名稱並使用新的數據庫來縮小此問題的范圍,並且還需要刪除“遷移”文件夾下的舊遷移文件。 另外,您可以參考adrian hall的有關實現代碼優先遷移的書。
所以我很沮喪地通過以下方式修復了它:
真是一場鬧劇。 顯然,我現在可以容納這個,但是當我的應用投入生產時,這將是一個重大問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.