簡體   English   中英

Azure移動服務和EF Code First-上下文已更改錯誤

[英]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的有關實現代碼優先遷移的書。

所以我很沮喪地通過以下方式修復了它:

  1. 將所有數據導出到查詢窗口,然后將結果保存到本地PC。
  2. 重命名舊數據庫
  3. 設置AutomaticMigrationsEnabled = true; 並重新部署
  4. 讓遷移創建數據庫(但這並沒有創建表)
  5. 設置AutomaticMigrationDataLossAllowed = true; #PanicModeOn
  6. 重新部署
  7. 重新導入我的所有數據

真是一場鬧劇。 顯然,我現在可以容納這個,但是當我的應用投入生產時,這將是一個重大問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM