【发布时间】:2015-09-29 23:23:53
【问题描述】:
我最近将一个现有项目迁移到 .net 4.5 并更改了该项目用于数据访问的内容(切换到实体框架)。
出于某种原因,任何时候我尝试访问 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.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; }
........
}
【问题讨论】:
-
MSDN 和我的对象浏览器(在 .NET 4.5 项目中)说命名空间是 System.Linq,它驻留在 System.Core.dll 中。
标签: c# .net entity-framework linq