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

不想你离开。 提交于 2019-12-04 01:53:00

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.

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