i have these classes:
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; }
}
In the context i have the following:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<EventComment>()
.HasMany(s => s.ChildComments)
.WithOptional()
.HasForeignKey(s => s.ParentCommentID);
}
The EventComments and children get loaded correctly. But every Event loads all EventComments with that EventID. The Event class should only load EventComments with no ParentID. How can i do that?
If I understand correctly, you're doing something like
var event = (from e in ctx.Event.Include(p=>p.EventComment) select e);
and you don't want all of the EventComments loaded, just specific ones.
Unfortunately EF does not provide a mechanism to filter the .Include.
You can however use projections as outlined here:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.