EF Core no .Include() method on DBset

.net-core asp.net-core entity-framework-core

Question

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?

1
19
5/23/2017 12:24:27 PM

Accepted Answer

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

42
5/23/2017 11:53:29 AM

Popular Answer

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.



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