How can I implement DBSet.AddOrUpdate in Entity Framework 4.4?

code-first entity-framework


In response to Slauma's answer to my question about running applications that use EF on Windows XP I am converting my application back from Entity Framework 5.0 to use Entity Framework 5.0 and target framework .NET 4.0 (Also called Entity Framework 4.4)

However I encounter the following error;

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 )

I have tried searching on fragments of this error message, but am not having much success. Strangely 4.4 isn't even mentioned in this Microsoft link There isn't even an SO tag for EF4.4

5/23/2017 12:17:07 PM

Accepted Answer

You must add...

using System.Data.Entity.Migrations; your code file to have AddOrUpdate available. It is an extension method of IDbSet<T> that is implemented in the IDbSetExtensions class in System.Data.Entity.Migrations namespace.

8/1/2013 4:17:41 PM

Popular Answer

When you enable migrations for MVC5 web applications, you get the following comment in the Seed method of the configuration:

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

My initial stab at this was to user DbSet<MyEntity>.AddOrUpdate(). This will lead to the same error message (and rightly so) as the one raised in this question. The fix is to read the rest of the comment and use the context parameter passed into the Seed function:


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow