DbSet不包含FirstOrDefault的定义吗? [英] DbSet doesn't contain definition for FirstOrDefault?

查看:424
本文介绍了DbSet不包含FirstOrDefault的定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently migrated an existing project to .net 4.5 and changed out what this project was using for data access (switching to Entity Framework).

由于某种原因,任何时候我尝试访问DbSet的任何功能(WhereFirstFirstOrDefault等)时,都会引发错误:

For some reason any time I try to access any functions for a DbSet (Where, First, FirstOrDefault, etc) it throws the error:

错误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指令或程序集引用?

Error 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' could be found (are you missing a using directive or an assembly reference?

这使用.net 4.5,并且我读到这些功能不再在System.Linq中,而是现在存储在System.Core中.我在项目中添加了对System.Core的引用,但仍然出现错误.对于System.Linq,有一个using语句,但对于System.Core,则没有.

This uses .net 4.5 and I read that these functions are no longer in System.Linq but are now stored in System.Core. I have added a reference to System.Core to the project but I am still getting the error. There is a using statement for System.Linq, but not for System.Core.

谁能看到我为什么会收到此错误?有关如何解决的建议?

Can anyone see why I would be getting this error? Suggestions on how to fix?

更新:

这是引发错误的行:

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

和我的DbContext:

and my DbContext:

public partial class VIModel : DbContext
{
     ........
     public virtual DbSet<Customer> Customers { get; set; }
     ........
}

推荐答案

The assembly for Queryable (the thing that adds the FirstOrDefault extension method you are using) is in System.Core, however it's namespace is System.Linq, you can see this on the MSDN page for it

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

Namespace: System.Linq
Assembly: System.Core (in System.Core.dll)

您需要在项目中引用System.Core,并且在文件中尝试使用它using System.Linq;

You need to have in your project a refrence to System.Core and in the file you are trying to use it a using System.Linq;

如果您同时拥有这两个内容,请仔细检查您要引用的项目或某些项目是否未创建它自己的System.Data.Entity.DbSet<T>类,该类未实现IQueryable<T>IEnumerable<T>.

If you have both of these things double check that your project or some project you are refrencing did not create it's own System.Data.Entity.DbSet<T> class which does not implement IQueryable<T> or IEnumerable<T>.

这篇关于DbSet不包含FirstOrDefault的定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆