DbSet不包含FirstOrDefault的定义?

问题描述 投票:5回答:2

我最近将一个现有项目迁移到.net 4.5,并更改了该项目用于数据访问的内容(切换到Entity Framework)。

出于某种原因,任何时候我尝试访问DbSet(WhereFirstFirstOrDefault等)的任何函数,它会抛出错误:

错误53'System.Data.Entity.DbSet1<MyProject.Data.Customer>' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Data.Entity.DbSet1'可以找到(您是否缺少using指令或程序集引用?

这使用.net 4.5,我读到这些函数不再在System.Linq中,但现在存储在System.Core中。我已经向项目添加了对System.Core的引用,但我仍然收到错误。 System.Linq有一个using语句,但System.Core没有。

谁能明白为什么我会收到这个错误?关于如何修复的建议?

更新:

这是抛出错误的行:

VIModel Db = new VIModel();
Customer = Db.Customers.FirstOrDefault(c => c.CustomerId == CustomerId && c.IsPrimary);

和我的DbContext:

public partial class VIModel : DbContext
{
     ........
     public virtual DbSet<Customer> Customers { get; set; }
     ........
}
c# .net entity-framework linq
2个回答
9
投票

Queryable的组件(添加你正在使用的FirstOrDefault扩展方法的东西)在System.Core中,但它的命名空间是System.Linq,你可以在MSDN页面上看到它

命名空间:System.Linq 程序集:System.Core(在System.Core.dll中)

您需要在项目中引用qazxsw poi并在文件中尝试使用它qazxsw poi

如果你有这两件事,请仔细检查你的项目或你正在进行的某个项目是不是创建了自己的System.Core类,它没有实现using System.Linq;System.Data.Entity.DbSet<T>


2
投票

我遇到了同样的问题。我尝试了以下解决方案来解决问题

  • 右键单击该项目。
  • 点击“Property Pages”。
  • 转到“构建”选项卡。
  • 设为“目标框架”4.5。
  • 尝试“构建”

我希望这个问题得到解决:)

© www.soinside.com 2019 - 2024. All rights reserved.