2013-01-24 64 views
7

下面是我用來嘗試幷包含User表的Lambda表達式,該表引發錯誤。包含派生模型相關類

ICollection<Activity> activity = db.Activities 
      .Include(i => i.Project.ProjectDoc.OfType<Cover>().Select(v => v.User)) 
      .Where(u => u.UserID == WebSecurity.CurrentUserId) 
      .OrderByDescending(d => d.DateCreated).ToList(); 

include語句給出了這樣的錯誤

包含路徑表達式必須是指在 所述類型定義的導航屬性。對於參考導航屬性使用虛線路徑,對集合導航屬性使用Select 運算符。

有問題的模型

public abstract class ProjectDoc 
{ 
    public int ProjectDocID { get; set; } 
    public int ProjectID { get; set; } 
    public string DocTitle { get; set; } 
    public string Status { get; set; } 
    public string Access { get; set; } 
    public DateTime DateCreated { get; set; } 


    public virtual ProjectDocAccess ProjectDocAccess { get; set; } 
    public virtual Project Project { get; set; } 
    public virtual ICollection<Comment> Comment { get; set; } 
    public ICollection<ProjectDocVote> ProjectDocVote { get; set; } 
} 
public class Segment : ProjectDoc 
{ 
    public string Content { get; set; } 
} 
public class Cover : ProjectDoc 
{ 
    public string CoverURL { get; set; } 
    public int UserID { get; set; } 
    public User User { get; set; } 
} 

怎樣包括UserCoverProjectDoc

UPDATE: 根據答案。我更新了Cover的模型,看起來像這樣,並刪除了我說的導致錯誤的內容。我現在可以得到的數據:

public class Cover : ProjectDoc 
{ 
    public string CoverURL { get; set; } 
    public int UserID { get; set; } 
    public virtual User User { get; set; } 
} 

回答

2

它目前是not supported。在派生類型上加載關係不切實際。你可以做的最好的是執行單獨的查詢來加載第一個查詢已經加載的所有需要​​的用戶,並讓EF做它的魔法(它應該填充已經加載實體的導航屬性,但是你必須關閉延遲加載)。