12

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).

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

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 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?

UPDATE:

This is the line that throws the error:

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

and my DbContext:

public partial class VIModel : DbContext
{
     ........
     public virtual DbSet<Customer> Customers { get; set; }
     ........
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • 1
    MSDN and my object browser (in a .NET 4.5 project) say the namespace is System.Linq and it resides in System.Core.dll. – Eric J. Sep 29 '15 at 23:27

3 Answers3

20

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

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

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;

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>.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • I do have the setup you described (reference to `.Core` and `using` `.Linq`) but the error still comes up. I looked around for other implementations of `DbSet` but there isn't anything. My DbContext does define a bunch of members like: `public virtual DbSet
    Addresses { get; set; }` where Address is a table in my DB, but that's it. Can you think of anything else that might be worth checking?
    – Abe Miessler Sep 30 '15 at 15:58
  • Can you update your question to include the line of code that causes the error to happen. – Scott Chamberlain Sep 30 '15 at 15:59
  • Sure - there are actually about 500 errors because it's being thrown anytime I try to use a function that would be defined in `System.Linq`. I posted one of the lines in my update. – Abe Miessler Sep 30 '15 at 16:17
  • Another interesting thing I noticed - `Customers` comes up in intellisense, but but when I type a period after that, nothing comes up in intellisense. Usually you will see the basic functions (`Equals`, `ToString`, etc), but nothing at all with this (and other EF) classes. – Abe Miessler Sep 30 '15 at 16:23
  • I'm using an instance (and also a weird naming convention). I get the confusion - I normally don't name variables with a capital letter, this is a naming convention started by someone else that I am continuing to use going forward. – Abe Miessler Sep 30 '15 at 16:36
  • Threw some of the code from my DbContext into the update as well – Abe Miessler Sep 30 '15 at 16:38
  • Yea, I rolled back the change. Out of curiosity do the intelisense methods show up if you do `DbSet test1 = null; test1.FirstOrDefault()`, also try `IQueryable test2 = null; test2.FirstOrDefault()`. If test2 works but test1 does not, hit F12 on the DbSet and see where it takes you. – Scott Chamberlain Sep 30 '15 at 16:39
  • 1
    Ok - so I went to do what you suggested and it said that `DbSet` was not found which I thought was strange. I checked and sure enough I didn't have `System.Data.Entities` added to my references. So I added that and had the same problem. Then I remembered that I had to install EntityFramework using NuGet for another project, so I did that and.... bingo! Everything started working! So if anyone else comes across this - `Install-Package EntityFramework` in your NuGet console fixes it – Abe Miessler Sep 30 '15 at 16:44
  • 1
    NOT SO INTUITIVE!! Thank you! :) – Piero Alberto Aug 09 '16 at 14:57
2

I was having the same problem. I tried the following solution to solve the problem

  • Right click on the project.
  • Click on "Property Pages".
  • Go to the "Build" tab.
  • Set to "Target Framework" 4.5.
  • Try "Build"

I hope this is resolved :)

Sedat Kumcu
  • 2,191
  • 1
  • 17
  • 13
-1

This question already has an answer but I had a different cause.

I also needed to have a reference to System before First or Default would show up...

tfcmad
  • 418
  • 1
  • 6
  • 20