Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core no .Include() method on DBset

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?

like image 451
rasmus91 Avatar asked Feb 09 '17 11:02

rasmus91


2 Answers

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! :)

like image 199
rasmus91 Avatar answered Sep 23 '22 08:09

rasmus91


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.

like image 37
Mike Devenney Avatar answered Sep 22 '22 08:09

Mike Devenney