DbSet doesn't contain definition for FirstOrDefault?

.net c# entity-framework linq

Question

I recently upgraded a current project to.net 4.5 and altered the data access method it utilized (switching to Entity Framework).

For some reason, if I attempt to access any DbSet (Where , First , FirstOrDefault , etc.), the error is thrown:

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?

This uses.net 4.5, and according to what I've read, these functions are now kept in System.Core rather than System.Linq. Despite adding a reference to System.Core to the project, the problem persists. System.Linq has a using statement, while System.Core does not.

Can somebody explain to me why I'm seeing this error? Any suggestions for a solution?

UPDATE:

The line that causes the mistake is as follows:

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

likewise my DbContext

public partial class VIModel : DbContext
{
     ........
     public virtual DbSet<Customer> Customers { get; set; }
     ........
}
1
6
2/28/2017 11:46:54 AM

Accepted Answer

The Queryable assembly (the component that adds theFirstOrDefault Your extension technique) is inSystem.Core Nevertheless, its namespace isSystem.Linq on the corresponding MSDN page, you can see this.

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

You must reference this in your project.System.Core and you are attempting to use it in a file.using System.Linq;

Verify that your project or the project you are refinancing did not originate on its own if you have both of these.System.Data.Entity.DbSet<T> class that is non-implementingIQueryable<T> or IEnumerable<T> .

12
9/30/2015 4:33:11 PM

Popular Answer

I experienced the same issue. I experimented with the following approach to the issue:

  • Place a right click on the project.
  • Then select "Property Pages."
  • Select the "Build" tab.
  • "Target Framework" 4.5 is selected.
  • Input "Build"

I hope this is resolved :)



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow