ras*_*s91 18 entity-framework-core .net-core asp.net-core
我目前完全无法调用.Include()和intellisense(在vscode中)似乎并不认为它存在.
现在经过很长一段时间搜索网络后,我发现了这个:
在我的EF实现Generic存储库中找不到.Include()方法
这似乎表明.Include仅存在于System.Data.Entities中,仅适用于EF 5和6.
那么我如何为EF核心中的实体加载我的列表属性?
继承我的背景
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");
}
}
Run Code Online (Sandbox Code Playgroud)
继承数据类:
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; }
}
Run Code Online (Sandbox Code Playgroud)
然后我做了类似的事情:
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();
}
Run Code Online (Sandbox Code Playgroud)
EF Core中不存在.Include吗?
ras*_*s91 37
这是我正在调用该方法的文件中缺少引用的直接结果(虽然我不太清楚我是怎么理解的......)
无论如何,添加:
using Microsoft.EntityFrameworkCore;
Run Code Online (Sandbox Code Playgroud)
像Tseng和Smit所说,做了诀窍.(在我定义函数的文件中)
虽然为什么有效但我不知道.我以为.include会自动通过DbSet提供.
谢谢!:)
如果你最终来到这里,一个 EF 6 或更低版本的用户碰巧错过了 OP 实际上像我一样提到这个,你想添加
using System.Data.Entity;
Run Code Online (Sandbox Code Playgroud)
到你的班级。
归档时间: |
|
查看次数: |
10795 次 |
最近记录: |