我有一个ticket
实体,它具有ticketNotes
(0到很多),并且希望在我查询票据时撤回谁创建每个笔记的用户详细信息。实体框架查询包含实体集合的子实体
我使用下面的代码来查询机票
var ticket = (from t in context.Tickets
.Include(t=>t.Site)
.Include(t=>t.Caller)
.Include(t=>t.Caller.Site)
.Include(t => t.Notes)
.Include(t=>t.OpenedByUser)
select t).First(t => t.TicketId == ticketId);
我TicketNote
类是:
public class TicketNote
{
public Guid TicketNoteId { get; set; }
public string NoteText { get; set; }
public DateTime CreatedTime { get; set; }
public Guid TicketId { get; set; }
public virtual Ticket Ticket { get; set; }
public bool ReadOnly { get; set; }
public DateTime? DateTimeDeleted { get; set; }
public Guid TenantId { get; set; }
[Required]
[DefaultValue(typeof(Guid), "00000000-0000-0000-0000-000000000000")]
public Guid CreatedByUserId { get; set; }
[ForeignKey("CreatedByUserId")]
public virtual User CreatedByUser { get; set; }
}
我想补充
.Include(t => t.Notes.CreatedByUser)
然而,由于票据是一个集合我没有得到选择。
请建议最好的方法来实现撤回目前为NULL的CreatedByUser。
感谢
不确定是否有'Any()'在这里工作 - '.Include(t => t.Notes.Any()。CreatedByUser)'? – Kami
嗨,谢谢我只是试过这个,但我没有任何实体选项,因此它不起作用。 –
也许这篇文章会帮助你:[链接](http://www.codeproject.com/Articles/788559/Loading-Related-Entities-with-Entity-Framework-A-B)。特别是有关显式加载的部分 – sszarek