2013-11-27 26 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