DBset 上的 EF Core 没有 .Include() 方法 [英] EF Core no .Include() method on DBset

查看:39
本文介绍了DBset 上的 EF Core 没有 .Include() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前完全无法调用 .Include() 并且智能感知(在 vscode 中)似乎不认为它存在.

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:

没有找到 .Include() 方法在我的EF 实现通用存储库

这似乎表明 .Include 仅存在于 System.Data.Entities 中,而后者仅适用于 EF 5 和 6.

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

那么我如何为 EF 核心中的实体急切加载我的列表属性?

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

这是我的背景

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");
    }
}

这里是数据类:

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();
}

EF Core 中不存在 .Include 吗?

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...)

无论如何,补充:

using Microsoft.EntityFrameworkCore;

喜欢 TsengSmit 建议,成功了.(在我定义函数的文件中)

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

虽然我不知道为什么会这样.我认为 .include 将通过 DbSet 自动可用.

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

不过还是谢谢!:)

这篇关于DBset 上的 EF Core 没有 .Include() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆