2013-11-27 28 views
2

選擇爲空,我有三個實體,如下實體框架:跳過幾列或相關實體

public partial class Ticket 
{ 
    public int TicketId { get; set; } 
    ... 
    public virtual ICollection<TicketComment> TicketComments { get; set; } 
} 

public partial class TicketComment 
{ 
    public int CommentId { get; set; } 
    public int TicketId { get; set; } 
    ... 
    public virtual ICollection<CommentAttachment> CommentAttachments { get; set; } 
    public virtual Ticket Ticket { get; set; } 
} 

public partial class CommentAttachment 
{ 
    public int FileId { get; set; } 
    public int CommentID { get; set; } 
    public string FileName { get; set; } 
    public int FileSize { get; set; } 
    public byte[] FileContents { get; set; } // holds large data 

    public virtual TicketComment TicketComment { get; set; } 
} 

這裏每張票可以有多個評論,每個評論可以有1或0附件。 我想急於負荷對於一個給定票所有相關實體與下面的代碼

var query = context.Tickets.Where(t => t.TicketId == ticketid) 
      .Include(t => t.TicketComments.Select(c => c.CommentAttachments)); 

它是做正確的工作。

唯一的問題是,它也加載byte[] FileContents,它往往有相當大的數據。我想避免它。

有沒有什麼辦法可以爲FileContents選擇NULL或者跳過本專欄?

我曾與以下

var query = context.Tickets.Where(t => t.TicketId == ticketid) 
      .Include(t => t.TicketComments 
       .Select(c => c.CommentAttachments 
        .Select(ca => new CommentAttachment() 
        { 
         CommentID = ca.CommentID, 
         FileContents = null, 
         FileId = ca.FileId, 
         FileName = ca.FileName, 
         FileSize = ca.FileSize 
        }))); 

嘗試,但它給錯誤

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path 

任何想法,以避免加載FileContents列?

回答

0
public partial class CommentAttachment 
{ 
    public int FileId { get; set; } 
    public int CommentID { get; set; } 
    public string FileName { get; set; } 
    public int FileSize { get; set; } 

    public virtual TicketComment TicketComment { get; set; } 
} 

public class FileContent 
{ 
    FileContentId {get;set;} 
    public int FileId { get; set; } // HERE IS THE FORGEIN KEY YOU HAVE TO UPDATE IT manually 
    public byte[] FileContents { get; set; } // holds large data 
} 

通過這種方式,你可以加載FileContent只需要你有CommentAttachment Id和可以將其包含的任何時間。

+0

這樣我必須改變模型,它也會影響項目的其他領域。沒有任何方法可以在不修改模型的情況下實現我想要的效果嗎? – SamTech

+0

試試這個公共字節?[] FileContents {get;組;它可能有效。 –

+0

我部分接受這個答案,因爲你建議修改模型。但是,它會將我的模型層次結構設置爲第4級。不同於增加4級層次結構,修改後的模型不同,所以它應該保持在3級。 – SamTech