42

I'm currently completely unable to call .Include() and intellisense (in vscode) doesn't seem to think it exists.

Now after a long time searching the web I've found this:

Not finding .Include() method in my EF implementing Generic repository

which seems to suggest that .Include exists only in System.Data.Entities, which is only available for EF 5 and 6.

So how do i eager load my list property for an entity in EF core?

heres my context

public class Database : DbContext { //Set new datasources like this: public DbSet<class> name { get; set; } public DbSet<Domain.Resource> Resources { get; set; } public DbSet<Domain.ResourceType> ResourceTypes { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Filename=./something.db"); } } 

Heres the data classes:

public class Resource { public int ResourceId { get; set; } public string Name { get; set; } public string Description { get; set; } public int ResourceTypeId { get; set; } public ResourceType ResourceType { get; set; } } public class ResourceType { public int ResourceTypeId { get; set; } public string Name { get; set; } public List<Resource> Resources { get; set; } } 

Then I do something like:

public List<ResourceType> GetAll() { var router = new Database(); var result = router.ResourceTypes.Include(rt => rt.Resources); //It's here there's absolutely no .Include method return result.ToList(); } 

Does .Include not exist in EF Core?

4
  • 6
    Did you hit Ctrl+. on the error message, so Visual Studio 2015/2017 can suggest you using Microsoft.EntityFrameworkCore namespace? ;) Commented Feb 9, 2017 at 11:28
  • I'm using visual studio code on Ubuntu Commented Feb 9, 2017 at 12:58
  • 1
    Just add using Microsoft.EntityFrameworkCore; & write above code. Even if intellisense cannot find it, it should compile properly. Commented Feb 10, 2017 at 1:55
  • Possible duplicate of Entity Framework Core does not contain a definition for 'Include' Commented Mar 28, 2018 at 6:14

3 Answers 3

91

It's a direct consequence of a missing reference in the file where I'm making a call to the method (though i'm not quite sure i understand how...)

Anyways, adding:

using Microsoft.EntityFrameworkCore; 

like Tseng and Smit suggested, did the trick. (in the file in which i define the function)

Though why that works i have no idea. I thought .include would automatically be available through the DbSet.

Thanks though! :)

Small, late EDIT: as Christian Johansen pointed out in his comment, the reason it needs the import to see the method signature, is that it is an extension method, which is a topic I strongly encourage any up-and-coming C# developer to learn about as it is immensely useful.

Sign up to request clarification or add additional context in comments.

1 Comment

Its implemented as an extention method, so its not visible until you include the namespace where the extention method is implemented
11

If you end up here, a user of EF 6 or below and happen to miss that OP actually mentioned this like I did, you want to add

using System.Data.Entity; 

to your class.

3 Comments

Why didn't VS suggest that to me (like other LINQ methods), it is very unintuitive to guess you miss that using.
Great question @GeorgiG, haven't dug into it in a while but I might because that has always bugged me.
@GeorgiG - a year later I happened back past this question and started digging a bit. Not sure why it didn't occur to me last year when you asked this and I responded, just not thinking I guess. Anyway, because it's an extension method it won't show until you add the namespace it hails from.
1

Here is a previous answer that is tracking this issue in EF7. It appears it is now 'included'.

4 Comments

I'm sorry, but I'm not looking for the overload of .Include that enables .Include(string) but the .Include(t => t.property). And by All accounts that was available already back in 2015, so why can't I access it at all I wonder :(
Its there. Starts on line 2162. The intellisense for VSCode can be more problematic than regular visual studio. I would start by checking that you have all the necessary extensions installed.
Hm. I'll try calling it then. EF core version 1.0 and beyond should be fine, right. I have to wonder what's with the intellisense though, as the only problem I've had with it so far is when I rename a class... And even then it's just a program restart away.
Try using it and see if your project compiles and runs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.