asp.net-core - EF Core 没有 DBset 上的 .Include() 方法

标签 asp.net-core .net-core entity-framework-core

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

经过长时间的网络搜索,我发现了这个:

Not finding .Include() method in my EF implementing Generic repository

这似乎表明 .Include 仅存在于 System.Data.Entities 中,该实体仅适用于 EF 5 和 6。

那么我如何急切地加载 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; }
}

然后我会做这样的事情:

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 吗?

最佳答案

这是我调用该方法的文件中缺少引用的直接结果(尽管我不太确定我理解如何...)

无论如何,添加:

using Microsoft.EntityFrameworkCore;

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

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

不过还是谢谢了! :)

小,晚编辑:如 Christian Johansen他在评论中指出,它需要导入才能查看方法签名的原因是它是一个扩展方法,这是我强烈鼓励任何有前途的 C# 开发人员学习的主题,因为它非常有用。

关于asp.net-core - EF Core 没有 DBset 上的 .Include() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42135149/

相关文章:

.net - Azure 中的环境(开发、生产等)在哪里设置?

c# - 是否可以在单独的类中移动层次结构的流畅配置?

c# - Asp.Net httpGet 获取 httpstatus 415 用于调用具有复杂类型参数的 Controller 方法

.net-core - OpenCover 未上传到 Azure Devops

c# - 中国地区应用洞察

c# - 如何使用 asp.net Core 下载文件?

entity-framework-core - Entity Framework 核心中的HasDefaultSchema在哪里?

.net-core - 如何在搭建数据库后运行迁移

asp.net-mvc - MVC Core 表单标签帮助器继承当前 View URL 的操作属性

c# - 注册模块 Autofac