✅ How to handle complex Include in EF Core
I have the following entity:
how could I arrange this to elegantly grab the info from the dbContext?
public class PlanEntity : ISortable {
public int Id { get; set; }
public int SortPriority { get; set; }
public string Label { get; set; }
public string PlanId { get; set; }
public List<TaskSetEntity> TaskSets { get; set; } = [];
public DateTime? StartDate { get; set; }
public DateTime? ReadyToTreatDate { get; set; }
public List<ApplicationUser> Authors { get; set; } = [];
public SiteEntity TreatmentSite { get; set; }
public TechniqueEntity Technique { get; set; }
public RegionEntity TreatedRegion { get; set; }
public Plan ToPlan() => new Plan {
Id = Id,
Label = Label,
Authors = Authors.Select(x => x.UserName).ToArray(),
TaskSets = TaskSets.Select(x => new TaskSet {
Id = x.Id,
Label = x.Label,
Colour = x.Color,
IsHidden = x.IsHidden,
Checks = x.Checks.ToChecks()}).ToArray(),
TreatedSite = TreatmentSite is null ? new Site { Name = string.Empty } : TreatmentSite.ToSite(),
Technique = Technique is null ? new Technique { Name = string.Empty } : Technique.ToTechnique(),
TreatedRegion = TreatedRegion is null ? new Region { Name = string.Empty } : TreatedRegion.ToRegion(),
};
}
public static class PlanExtensions {
public static Plan[] ToPlans(this List<PlanEntity> entities) => entities
.OrderBy(x => x.SortPriority)
.Select(x => x.ToPlan())
.ToArray();
}public class PlanEntity : ISortable {
public int Id { get; set; }
public int SortPriority { get; set; }
public string Label { get; set; }
public string PlanId { get; set; }
public List<TaskSetEntity> TaskSets { get; set; } = [];
public DateTime? StartDate { get; set; }
public DateTime? ReadyToTreatDate { get; set; }
public List<ApplicationUser> Authors { get; set; } = [];
public SiteEntity TreatmentSite { get; set; }
public TechniqueEntity Technique { get; set; }
public RegionEntity TreatedRegion { get; set; }
public Plan ToPlan() => new Plan {
Id = Id,
Label = Label,
Authors = Authors.Select(x => x.UserName).ToArray(),
TaskSets = TaskSets.Select(x => new TaskSet {
Id = x.Id,
Label = x.Label,
Colour = x.Color,
IsHidden = x.IsHidden,
Checks = x.Checks.ToChecks()}).ToArray(),
TreatedSite = TreatmentSite is null ? new Site { Name = string.Empty } : TreatmentSite.ToSite(),
Technique = Technique is null ? new Technique { Name = string.Empty } : Technique.ToTechnique(),
TreatedRegion = TreatedRegion is null ? new Region { Name = string.Empty } : TreatedRegion.ToRegion(),
};
}
public static class PlanExtensions {
public static Plan[] ToPlans(this List<PlanEntity> entities) => entities
.OrderBy(x => x.SortPriority)
.Select(x => x.ToPlan())
.ToArray();
}how could I arrange this to elegantly grab the info from the dbContext?