我最近将一个现有项目迁移到.net 4.5,并更改了该项目用于数据访问的内容(切换到Entity Framework)。
出于某种原因,任何时候我尝试访问DbSet(Where
,First
,FirstOrDefault
等)的任何函数,它会抛出错误:
错误53'System.Data.Entity.DbSet
1<MyProject.Data.Customer>' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Data.Entity.DbSet
1'可以找到(您是否缺少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; }
........
}
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>
。
我遇到了同样的问题。我尝试了以下解决方案来解决问题
我希望这个问题得到解决:)