Asp.net-Core
EF Core 沒有 DBset 上的 .Include() 方法
我目前完全無法呼叫 .Include() 並且智能感知(在 vscode 中)似乎不認為它存在。
現在在網上搜尋了很長時間後,我發現了這個:
在我的 EF 實現通用儲存庫中找不到 .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"); } }繼承人的數據類:
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(); }.Include 在 EF Core 中不存在嗎?
這是我正在呼叫該方法的文件中缺少引用的直接後果(儘管我不太確定我理解如何……)
無論如何,添加:
using Microsoft.EntityFrameworkCore;就像Tseng和Smit建議的那樣,成功了。(在我定義函式的文件中)
雖然我不知道為什麼會這樣。我以為
.include會自動通過DbSet.不過謝謝!:)