EF Core no .Include() method on DBset

孤街浪徒 提交于 2020-05-25 06:02:32

问题


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:


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




回答2:


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:


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



来源:https://stackoverflow.com/questions/42135149/ef-core-no-include-method-on-dbset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!