2013-08-01 31 views
11

針對Slauma的回答我的問題about running applications that use EF on Windows XP我5.0轉換我的應用程序恢復從實體框架使用實體框架5.0和目標框架.NET 4.0(也稱爲實體框架4.4)如何在實體框架4.4中實現DBSet.AddOrUpdate?

但是我遇到下面的錯誤;

System.Data.Entity.DbSet<MyEntity> does not contain a definition for AddOrUpdate 
and no extension method of a type System.Data.Entity.DbSet<MyEntity> accepting a 
first argument of type System.Data.Entity.DbSet<MyEntity> could be found. 
(Are you missing a using directive or assembly reference) 

我試過在這個錯誤消息的片段上搜索,但沒有太大的成功。 奇怪的是4.4甚至沒有提及in this Microsoft link甚至沒有對EF4.4

回答

29

您必須添加一個SO標籤...

using System.Data.Entity.Migrations; 

...您的代碼文件有AddOrUpdate可用。它是在System.Data.Entity.Migrations名稱空間中的IDbSetExtensions類中實現的extension method of IDbSet<T>

0

當你enable migrations爲MVC5 Web應用程序,你在配置種子方法如下評論:

// You can use the DbSet<T>.AddOrUpdate() helper extension method 

我在這最初的刺是用戶DbSet<MyEntity>.AddOrUpdate()。這將導致與此問題中提出的相同的錯誤信息(並且正確)。 解決的辦法是讀取其餘的註釋並使用傳遞給Seed函數的上下文參數:

context.MyEntity.AddOrUpdate();