EF框架CodeFirst the model backing the 'PModelEntities' context has changed since the database was created. Consider using Code First Migrations to update the database


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.如果發現有問題,重新生成解決方案

下面時操作時的照片:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM