我目前完全无法调用 .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;
喜欢Tseng和 Smit建议,成功了。 (在我定义函数的文件中)
虽然我不知道为什么会这样。我认为 .include
将自动通过 DbSet
可用。
不过还是谢谢了! :)
小,晚编辑:如 Christian Johansen他在评论中指出,它需要导入才能查看方法签名的原因是它是一个扩展方法,这是我强烈鼓励任何有前途的 C# 开发人员学习的主题,因为它非常有用。
关于asp.net-core - EF Core 没有 DBset 上的 .Include() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42135149/