This answer was useful in updating child entities when a parent is updated. I'm now trying to also update a child of a child. The below works for finding what child records, if any, belong to a parent so records can be added/updated.
var calFile = await innerContext.Calibrate
.Where(x => x.DataId == dataFile.Id)
.Include(x => x.Symptom)
.SingleOrDefaultAsync();
This does not work for adding/updating the child of a child:
var calFile = await innerContext.Calibrate
.Where(x => x.DataId == dataFile.Id)
.Include(x => x.Symptom)
.Include(x => x.SymptomQuestions
.SingleOrDefaultAsync();
My entities are as follows:
public class Calibrate : IAnalyticsSection
{
public int Id { get; set; }
public Guid DataFileId { get; set; }
public bool TestType { get; set; }
public decimal Height { get; set; }
public decimal CalibratedHeadPosition { get; set; }
public decimal LeftHandPositionTPose { get; set; }
public decimal RightHandPositionTPose { get; set; }
public decimal LeftHandSpherePos { get; set; }
public decimal RightHandSpherePos { get; set; }
public ICollection<Symptom> Symptoms { get; set; } = new List<Symptom>();
public virtual DataFile DataFile { get; set; }
}
public class Symptom
{
public int Id { get; set; }
public int CalibeId { get; set; }
public int SymptomSeverity { get; set; }
public virtual ICollection<SymptomQuestions> SymptomQuestions { get; set; } = new List<SymptomQuestions>();
public virtual Calibrate Calibrate { get; set; }
}
public class SymptomQuestions
{
public int Id { get; set; }
public int SymptomsId { get; set; }
public int Question { get; set; }
public int Answer { get; set; }
public virtual Symptom Symptoms { get; set; }
}
Calibrate can have several Symptoms, each of which will have 5 questions.
How can this be done?
Maybe it's as simple as using .ThenInclude(..)
:
var calFile = await innerContext.Calibrate
.Where(x => x.DataId == dataFile.Id)
.Include(x => x.Symptom)
.ThenInclude(x => x.SymptomQuestions)
.SingleOrDefaultAsync();
But maybe we need to see the definition of the relations (FluentAPI?), too?
Or your issue is related to a recent breaking change of EF Core 5.0 ( Non-null reference navigations are not overwritten by queries ): .Include(...)
will only set related items from the database, if your navigation property is still null. Since you initialize Symptoms
and SymptomsQuestions
here, .Include()
might just do nothing.
Side note: Why call one entity "Symptom" (singular) and the other "SymptomQuestion s " (= plural)? Both seem to represent just one instance.
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.