Azure + EF 6 - 使代碼首次遷移工作

asp.net-mvc asp.net-mvc-5 azure entity-framework entity-framework-6

我正在使用EF 6(代碼優先遷移)用於MVC 5項目。在我的本地DEV機器上一切正常。

但是當我將項目部署到Azure時,當我的應用程序首次嘗試與數據庫交互時,我收到以下錯誤:

Migrations is enabled for context 'UtilitiesContext' but the database does not exist or
contains no mapped tables. Use Migrations to create the database and its tables, for
example by running the 'Update-Database' command from the Package Manager Console.

我在Utilities.Data程序集中有我的EF相關代碼,我的MVC項目在Utilities.Web程序集中。

這是我的代碼供您參考:

UtilitiesContext.cs

Migrations is enabled for context 'UtilitiesContext' but the database does not exist or
contains no mapped tables. Use Migrations to create the database and its tables, for
example by running the 'Update-Database' command from the Package Manager Console.

Configuration.cs

Migrations is enabled for context 'UtilitiesContext' but the database does not exist or
contains no mapped tables. Use Migrations to create the database and its tables, for
example by running the 'Update-Database' command from the Package Manager Console.

Settings.cs

Migrations is enabled for context 'UtilitiesContext' but the database does not exist or
contains no mapped tables. Use Migrations to create the database and its tables, for
example by running the 'Update-Database' command from the Package Manager Console.

我在App Settings Configuration選項卡的“ App Settings部分中定義了此內容:

鍵:DB.ConnectionString

值:Data Source = tcp:host.database.windows.net,1433; Initial Catalog = Utilities; User Id = user @ server; Password = pwd;

熱門答案

您遇到的問題似乎是您將應用程序部署到Azure,但它嘗試定位的數據庫尚不存在。

生產代碼的最佳解決方案是從本地計算機上的程序包管理器控制台運行以下命令,這將生成一個sql腳本,您可以對生產數據庫運行該腳本以使其與您的應用程序保持同步。

PM> Update-Database -script -sourceMigration 0

或者,如果您只想在azure中進行測試,MigrateDatabaseToLatestVersion初始化程序將執行您所需的操作,並確保在您的應用程序啟動時數據庫是最新的。通常不建議在生產代碼中使用初始化程序,因為更改生產數據庫應該非常謹慎。

這篇博文有點陳舊,但在初始化和遷移方面做得很好。




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因