3

如何从已加载的集合中加载相关实体:

收藏:

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; }
}

我需要为广告中的所有功能加载实体关键字。

谢谢

4

1 回答 1

0

在您的存储库类中尝试:

public Ad GetAd(int id)
{
     return _database.Set<Ad>().Include(ad => ad.Features.Select(feature => feature.Keyword)).FirstOrDefault(ad => ad.Id == id);
}
于 2013-07-13T00:44:59.347 回答