繁体   English   中英

实体框架:跳过几列或从相关实体中选择为空

[英]Entity Framework: Skip few columns or select as null from related entities

我有三个实体如下

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列?

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
}

通过这种方式,您只需要拥有CommentAttachment Id即可加载FileContent,并且您可以随时包含它。

返回null getter但允许setter在EF 6.0和OData v4中运行。

我遇到过同样的问题。 接受的答案肯定是一个可行的选择,我今天几乎实现了它,但我希望在代码中执行任务。 我的模型略有不同,因为byte[] Timestamp列可用。 如果以后可以帮助某人,则发布此解决方法。

对于我的情况, byte[] FileContents用作目录存储库的备份,如果前端因各种原因被清除,我们使用数据库来重建目录结构和文件内容。 基本上我们只有POST / SET FileContents,我们从未通过前端程序读取它。

public partial class CommentAttachment
{
     //required for overriding get/set auto-property
     private byte[] _FileContents; 

     public int FileId { get; set; }
     public int CommentID { get; set; }
     public string FileName { get; set; }
     public int FileSize { get; set; }
     public byte[] FileContents 
     { 
          get
          {
               return Timestamp != null ? null : _FileContents;
          } 
          set
          {
               _FileContents = value;
          }
     }  // holds large data

     public virtual TicketComment TicketComment { get; set; }
     //Concurrency Token - triggered on create or update
     public byte[] Timestamp { get; set; }
 }

上面允许我们的数据库表保持不变,只有EF POCO改变了。 创建CommentAttachment记录时, Timestamp字段为null /不存在,因为它是由数据库触发的; 这允许填充_FileContents。 选择记录时,会使用数据库中的值填充Timestamp ,因此将FileContents设置为null以避免查询大型数据集。

当应用于OData v4时,PUT / PATCH / POST也可以正常使用此方法,因为它不是为了插入/更新而传递Timestamp ,因为它是数据库触发的填充。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM