2013-07-12 39 views
0

說我的對象圖的樣子:如何急切地包括導航性能的子對象

User 
=> Friends 
    => Clicks 
    => Urls 

所以,當我打開一個用戶,我也想eagirly加載導航屬性朋友。而且我希望朋友對象能夠輕鬆加載點擊等。

使用代碼我現在我只能爲1級做到這一點:

public User GetUserById(int userId) 
{ 
    return Get(x => x.Id == userId, includeProperties: "Friends").FirstOrDeafult(); 
} 

這可能嗎?

回答

1

顯然,這個倉庫實現就象是分割用逗號(includeProperties.Split(new char[] { ',' })的includeProperties參數存儲庫模式,然後調用

query = query.Include(includeProperty); 

對於拆分結果數組中的每個元素。爲了您的例子中,你可以使用一個虛線路徑則:

return Get(x => x.Id == userId, includeProperties: "Friends.Clicks.Urls") 
    .FirstOrDefault(); 

它會加載所有實體的路徑上從根User實體到最後的導航屬性Urls

如果你有在User另一個導航屬性 - 說Profile - 你會想急切地加載,以及,似乎這句法支持則:

return Get(x => x.Id == userId, includeProperties: "Profile,Friends.Clicks.Urls") 
    .FirstOrDefault(); 
+0

怎麼樣設置排序依據爲孩子他們加載時的集合?這可能嗎? – loyalflow

+0

@ user1361315:這不是直接可能的,只有一些解決方法:http://stackoverflow.com/questions/7522784/ef-4-1-code-first-how-to-order-navigation-properties-when-using -include和 - 或 – Slauma

相關問題