繁体   English   中英

从实体框架中的集合加载相关实体

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM