目前我完全无法调用 .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吗?
Ctrl+.
,这样Visual Studio 2015/2017就会建议你使用Microsoft.EntityFrameworkCore
命名空间了吗? ;) - Tsengusing Microsoft.EntityFrameworkCore;
并在上面编写代码。即使智能感知无法找到它,它也应该可以正确编译。 - Smit