1. 程式人生 > >Entity Framework : The model backing the '' context has changed since the database was created

Entity Framework : The model backing the '' context has changed since the database was created

解決方案 als state exp 操作 enabled chan question migration

1.采用code first 做項目時,數據庫已經生成,後期修改數據庫表結構。再次運行時出現一下問題:

Entity Framework : The model backing the ‘ProductModel‘ context has changed since the database was create

解決方法:

1.打開當前項目中的:程序包管理器控制臺

技術分享

2.輸入:enable-migrations -ProjectName ‘ProductModel‘ -Force

解釋:’ProductModel‘,ef框架model層所在程序集名稱

看到執行結果:

Checking if the context targets an existing database...


Code First Migrations enabled for project ProductModel.

2.打開 ’ProductModel‘,找到Migrations-Configuration

public Configuration()
{
    AutomaticMigrationsEnabled = true; (原來為false,改為true)
AutomaticMigrationDataLossAllowed = false; (是否丟數據)
}

3.輸入:update-DataBase -ProjectName ‘ProductModel‘ -Force

看到執行結果:

Specify the ‘-Verbose‘ flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
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.


You can use the Add-Migration command to write the pending model changes to a code-based migration.

4.如果發現有問題,重新生成解決方案

下面時操作時的照片:

技術分享

技術分享

Entity Framework : The model backing the '' context has changed since the database was created