DBset上的EF Core no .Include()方法 [英] EF Core no .Include() method on DBset
问题描述
我目前完全无法调用.include(),并且intellisense(在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:
在我的EF实现通用存储库的EF中找不到.include()方法
这似乎表明。 Include仅存在于System.Data.Entities中,仅适用于EF 5和EF 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; }
}
然后我做类似的事情:
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中不存在?
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;
像 Tseng 和 Smit 提出了建议。 (在我定义函数的文件中)
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 no .Include()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!