2013-07-13 173 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