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

允我心安 提交于 2019-12-21 07:15:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!