跳到主要內容

發表文章

目前顯示的是有「Entity Framework」標籤的文章

Entity Framework 5 Code First 遇到 context has changed 的問題

我在做一個Asp.net MVC 4 + Entity Framework 5 RC的專案遇到一個問題,就是新增加的Model做 當初是Code First開發,而且我什麼都沒做就很正常地運作。但是後來我再加上一個Model Class後,居然就不會自動更新。錯誤訊息是 The model backing the 'XXXXContext' context has changed since the database was created. Consider using Code First Migrations to update the database ( http://go.microsoft.com/fwlink/?LinkId=238269 ) 那個連結會導到 這裡 ,不完全解答我的問題。 這裡 有高手介紹Entity Framework 4.3.1的做法。 我就想說Let it be,不要再繼續Code First,接下來手動就好,找到在DbContext Class加上這個method protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.IncludeMetadataInDatabase = false; } 很可惜,在Entity Framework 5.0 RC改了,所以要寫成 protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>(); } 這樣就不會去檢查資料庫是否比Context舊。 Update: 不知為何又不行了,只好回過頭找Package Manager Console失敗的原因,發現packages不知何時多裝一個EntityFramework 4.3.1,砍掉之後重新啟動Visual Studio,後來就可以正常Mirgation。