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?
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! :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With