14

为了回应 Slauma 对我关于在 Windows XP 上运行使用 EF 的应用程序的问题的回答,我 将我的应用程序从 Entity Framework 5.0 转换回使用 Entity Framework 5.0 和目标框架 .NET 4.0(也称为 Entity Framework 4.4)

但是我遇到以下错误;

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 )

我曾尝试搜索此错误消息的片段,但没有取得多大成功。奇怪的是,这个 Microsoft 链接中甚至没有提到 4.4 EF4.4 甚至没有 SO 标签

4

2 回答 2

33

您必须添加...

using System.Data.Entity.Migrations;

...到您的代码文件AddOrUpdate可用。它是在命名空间中的类中实现的扩展方法IDbSet<T>IDbSetExtensionsSystem.Data.Entity.Migrations

于 2013-08-01T16:17:41.300 回答
4

当您为 MVC5 Web 应用程序启用迁移时,您会在配置的 Seed 方法中获得以下注释:

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

我最初的尝试是针对 user DbSet<MyEntity>.AddOrUpdate()。这将导致与此问题中提出的错误消息相同(并且正确)。修复方法是阅读注释的其余部分并使用传递给 Seed 函数的上下文参数:

    context.MyEntity.AddOrUpdate();
于 2015-07-21T04:04:23.440 回答