问题
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
回答1:
You must add...
using System.Data.Entity.Migrations;
...to 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.
回答2:
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:
context.MyEntity.AddOrUpdate();
来源:https://stackoverflow.com/questions/17991766/how-can-i-implement-dbset-addorupdate-in-entity-framework-4-4