[英]Load related entities from a Collection in Entity Framework
如何從已加載的Collection中加載相關實體:
收藏品:
public class Ad
{
// Primary properties
[Key]
public int Id { get; set; }
private ICollection<Feature> _features;
public virtual ICollection<Feature> Features
{
get { return _features ?? (_features = new HashSet<Feature>()); }
set { _features = value; }
}
}
特色:
public class Feature
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
// Navigation properties
public virtual ICollection<Ad> Ads { get; set; }
public Keyword Keyword { get; set; }
}
關鍵字:
public class Keyword
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
我需要為廣告中的所有功能加載實體關鍵字。
謝謝
在您的存儲庫類中嘗試:
public Ad GetAd(int id)
{
return _database.Set<Ad>().Include(ad => ad.Features.Select(feature => feature.Keyword)).FirstOrDefault(ad => ad.Id == id);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.