2013-07-13 162 views
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; } 
} 

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

感谢

回答

0

在你的资料库类的尝试:

public Ad GetAd(int id) 
{ 
    return _database.Set<Ad>().Include(ad => ad.Features.Select(feature => feature.Keyword)).FirstOrDefault(ad => ad.Id == id); 
} 
+0

感谢大家好,这里的问题是,我使用存储库与获取,广告等方法。 – Patrick

+0

@Patrick,主要思想是'.Include(ad => ad.Features.Select(feature => feature.Keyword))''。或者你可以展示一个例子你的存储库是如何实现的? – Dmytro