日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

EntityFramework项目“The model backing xxx context has changed ”错误解决-CodeFirst迁移踩坑记录

發(fā)布時間:2024/1/1 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EntityFramework项目“The model backing xxx context has changed ”错误解决-CodeFirst迁移踩坑记录 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文為本人日記,用于個人記錄。

報錯現(xiàn)象:

The model backing the 'XXXXContext' context has changed since the database was created. Consider using Code First Migrations to update the database?

?

環(huán)境及背景:

接收古董項目10年前的代碼,攤上傳說中的祖?zhèn)鞔a,踩坑無數(shù),今天說說其中一個坑。個人是不喜歡EF的,做個Demo還行,真實項目上使用靈活性太差。

古董代碼環(huán)境:VS2010+EF4.1+WCF+C#+SQLServer2008+WinForm+C++_硬件API+硬件

需求:數(shù)據(jù)庫不能重建,在老庫(已有數(shù)據(jù))某兩個數(shù)據(jù)表增加幾個字段。(注意需求是已有數(shù)據(jù))

工程組成:Model層、WCFService層、WinFormClient層

?

踩坑場景還原:

針對想當然做法:Model層對表對應(yīng)實體中增加字段、數(shù)據(jù)庫中手動以SQL增加兩個列、更新WCF引用,坐等測試通過

運行現(xiàn)象:WinFormClient調(diào)用WCF,在WCFService層非改動表使用using (XXDBContext db = new XXDBContext())拋出異常,異常消息:The model backing the 'XXXXContext' context has changed since the database was created. Consider using Code First Migrations to update the database?

不好,大事不妙,悟空壞了蟠桃大會,后果很嚴重,黎叔很生氣。上下求索的路子曲折,這里不贅述了。

?

前提:重要的事情說三遍,備份數(shù)據(jù)庫備份源碼X3

?

解決辦法:

關(guān)鍵思想:CodeFirst模式EF代碼與數(shù)據(jù)庫形成了綁定關(guān)系,手動修改數(shù)據(jù)庫增加列后,EF不認。通過EF CodeFirst遷移方式,讓EF對數(shù)據(jù)表字段新增修改。

?

步驟

  • Vs2010無法聯(lián)網(wǎng),通過瀏覽器到nuget官網(wǎng)https://www.nuget.org下載entityframework.4.3.1的nuget包【entityframework.4.3.1.nupkg】,準備離線安裝。(注意,不要下載EntityFramework 4.1.10331包,因為老版本EF不支持“Enable-migrations”命令,會報【無法將“Enable-migrations”識別為cmdlet】)
  • vs2010中查看或設(shè)置程序包源,本例,將entityframework.4.3.1.nupkg拷貝到C:\Users\Administrator\AppData\Roaming\NuGet\Cache
  • 程序包源選擇local,默認項目選擇實體類工程, vs2010程序包管理控制臺輸入命令: Install-Package EntityFramework -Version 4.3.1
  • vs2010程序包管理控制臺輸入命令:Enable-Migrations
  • 提示錯誤,根據(jù)錯誤得知此命令想要訪問數(shù)據(jù)庫。
  • 在實體類工程中增加app.config添加數(shù)據(jù)庫連接字符串,如下 <?xml version="1.0" encoding="utf-8"?> <configuration><configSections><section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections><entityFramework><defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"><parameters><parameter value="Data Source=XX;Initial Catalog=XX;user id=XXX;password=XXX" /></parameters></defaultConnectionFactory></entityFramework> </configuration>

    ?

  • 程序包管理控制臺再次輸入啟用遷移的命令Enable-Migrations,本例前面嘗試過幾次所以使用Enable-Migrations -Force

  • PM> Enable-Migrations Migrations have already been enabled in project 'EntityModel'. To overwrite the existing migrations configuration, use the -Force parameter. PM> Enable-Migrations -Force Code First Migrations enabled for project EntityModel.

    ?

  • 把實體類工程EntityModel設(shè)置為啟動項目

  • 再次執(zhí)行命令A(yù)dd-Migration AddCol_Table。如果仍報數(shù)據(jù)庫連接錯誤,則可嘗試使用“

    Add-Migration AddCol_Table -ConnectionString "Data Source=127.0.0.1;Initial Catalog=xxx;user id=xx;password=xx"

    位于命令管道位置 1 的 cmdlet Add-Migration

    請為以下參數(shù)提供值:

    ConnectionProviderName: System.Data.SqlClient

    Scaffolding migration 'AddCol_C_RoutinePower'.

  • ?

    此命令執(zhí)行后會在當前工程下創(chuàng)建一個202008280232342_AddCol_Table.cs文件,文件代碼大致類似如下 namespace EntityModel.Migrations {using System.Data.Entity.Migrations;public partial class AddCol_Table: DbMigration{public override void Up(){CreateTable("xxx",c => new{xxxx}).PrimaryKey(t => t.xxx);}public override void Down(){DropIndex("xxx", new[] { "cc" });DropForeignKey("xxx", "bb", "aa");DropTable("xxx");}} }

    ?

  • 我們不需要上面的刪除表和新增表代碼,修改代碼為新增字段刪除字段

    namespace EntityModel.Migrations {using System.Data.Entity.Migrations;public partial class AddCol_C_PowerCalibrateBase : DbMigration{public override void Up(){//下面的代碼是wdh手動添加的。AddColumn("dbo.xxx", "aaa", c => c.Double(nullable: null));}public override void Down(){DropColumn("dbo.xxx", "aaa");}} }

    ?

  • 輸入命令,用指定連接字符串將上述代碼更新到數(shù)據(jù)庫。命令:update-database -verbose -ConnectionString "Data Source=127.0.0.1;Initial Catalog=xxxx;user id=xxx;password=xxx",在提示后輸入“System.Data.SqlClient”

  • PM> update-database -verbose -ConnectionString "Data Source=127.0.0.1;Initial Catalog=xxxx;user id=xxx;password=xxx" 位于命令管道位置 1 的 cmdlet Update-Database 請為以下參數(shù)提供值: ConnectionProviderName: System.Data.SqlClient Using NuGet project 'EntityModel'. Using StartUp project 'EntityModel'. Target database is: 'xxx' (DataSource: 127.0.0.1, Provider: System.Data.SqlClient, Origin: Explicit). Applying explicit migrations: [202008280232342_AddCol_Table]. Applying explicit migration: 202008280232342_AddCol_xxx. ALTER TABLE [dbo].[xxx] ADD [xxx] [float] ALTER TABLE [dbo].[xxx] ADD [xxx] [float] CREATE TABLE [__MigrationHistory] ([MigrationId] [nvarchar](255) NOT NULL,[CreatedOn] [datetime] NOT NULL,[Model] [varbinary](max) NOT NULL,[ProductVersion] [nvarchar](32) NOT NULL,CONSTRAINT [PK___MigrationHistory] PRIMARY KEY ([MigrationId]) ) BEGIN TRYEXEC sp_MS_marksystemobject '__MigrationHistory' END TRY BEGIN CATCH END CATCH [Inserting migration history record] PM>

    ?

  • 檢查數(shù)據(jù)庫中表字段是否已經(jīng)新增成功了。

  • 如果update-database命令執(zhí)行未報錯,前面已執(zhí)行過多次,但數(shù)據(jù)庫中對應(yīng)的表字段未加上。處理辦法:將[dbo].[__MigrationHistory]表中的與當前記錄重復(fù)202008280307106_AddCol_Table記錄刪除,然后重新執(zhí)行一遍update-database命令。

  • 打開客戶端,重新調(diào)用WCF,OK,通過。

  • ?

    官方參考

    https://docs.microsoft.com/zh-cn/ef/ef6/modeling/code-first/migrations/?redirectedfrom=MSDN

    參考:
    https://www.cnblogs.com/chenwolong/p/migrations.html

    https://www.cnblogs.com/haogj/archive/2012/02/17/2356537.html

    https://www.cnblogs.com/Laimic/articles/4318707.html

    https://blog.csdn.net/hit_why/article/details/72778785?utm_source=blogxgwz28

    nuget包:

    https://www.nuget.org/packages/EntityFramework/

    總結(jié)

    以上是生活随笔為你收集整理的EntityFramework项目“The model backing xxx context has changed ”错误解决-CodeFirst迁移踩坑记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。