我有这些课程:
public class Event
{
[Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public DateTime Begin { get; set; }
[Required]
public DateTime End { get; set; }
public string Description { get; set; }
public virtual ICollection<EventRegistration> Registrations { get; set; }
public virtual ICollection<EventComment> Comments { get; set; }
}
public class EventComment
{
[Key]
public int ID { get; set; }
[Required]
public int PersonID { get; set; }
[Required]
public int EventID { get; set; }
[Required]
public DateTime EntryDate { get; set; }
[Required]
public string Comment { get; set; }
public int? ParentCommentID { get; set; }
public virtual Event CommentEvent { get; set; }
public virtual Person CommentPerson { get; set; }
public virtual EventComment ParentComment { get; set; }
public virtual ICollection<EventComment> ChildComments { get; set; }
}
在上下文中我有以下内容:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<EventComment>()
.HasMany(s => s.ChildComments)
.WithOptional()
.HasForeignKey(s => s.ParentCommentID);
}
EventComments和子项正确加载。 但是每个事件都会使用该EventID加载所有EventComments。 Event类只应加载没有ParentID的EventComments。 我怎么能这样做?
答案 0 :(得分:0)
如果我理解正确,你会做类似
的事情var event = (from e in ctx.Event.Include(p=>p.EventComment) select e);
并且您不希望所有加载的EventComments,只是特定的
。不幸的是,EF没有提供过滤.Include的机制。
然而,您可以使用此处概述的预测: