Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用实体框架核心在C#中添加/更新子级的子级?_C#_.net_Linq_Entity Framework Core - Fatal编程技术网

如何使用实体框架核心在C#中添加/更新子级的子级?

如何使用实体框架核心在C#中添加/更新子级的子级?,c#,.net,linq,entity-framework-core,C#,.net,Linq,Entity Framework Core,当父实体更新时,答案在更新子实体时很有用。我现在也在尝试更新一个孩子的孩子。以下内容用于查找属于父记录的子记录(如果有),以便添加/更新记录 var calFile = await innerContext.Calibrate .Where(x => x.DataId == dataFile.Id) .Include(x => x.Symptom) .SingleOrDefaultAsync();

当父实体更新时,答案在更新子实体时很有用。我现在也在尝试更新一个孩子的孩子。以下内容用于查找属于父记录的子记录(如果有),以便添加/更新记录

var calFile = await innerContext.Calibrate
    .Where(x => x.DataId == dataFile.Id)
    .Include(x => x.Symptom)                              
    .SingleOrDefaultAsync();
这不适用于添加/更新子对象的子对象:

var calFile = await innerContext.Calibrate
    .Where(x => x.DataId == dataFile.Id)
    .Include(x => x.Symptom) 
    .Include(x => x.SymptomQuestions                             
    .SingleOrDefaultAsync();
我的实体如下:

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; }

}
公共类校准:IAnalyticsSection
{
公共int Id{get;set;}
公共Guid DataFileId{get;set;}
公共bool测试类型{get;set;}
公共十进制高度{get;set;}
公共十进制校准头位置{get;set;}
公共十进制LeftHandPositionPose{get;set;}
公共十进制RightHandPositionPose{get;set;}
公共十进制LeftHandSpherePos{get;set;}
公共十进制RightHandSpherePos{get;set;}
公共ICollection症状{get;set;}=new List();
公共虚拟数据文件数据文件{get;set;}
}
公共类症状
{
公共int Id{get;set;}
public int CalibeId{get;set;}
公共整数{get;set;}
公共虚拟ICollection{get;set;}=new List();
公共虚拟校准{get;set;}
}
公开课问题
{
公共int Id{get;set;}
public int sid{get;set;}
公共整数问题{get;set;}
公共整数应答{get;set;}
公共虚拟症状{get;set;}
}
校准可能有几个症状,每个症状都有5个问题


如何做到这一点?

也许这就像使用
一样简单。然后包括(…)

但也许我们也需要看看关系的定义(FluentAPI?)

或者您的问题与EF Core 5.0()最近的突破性更改有关:
.Include(…)
将仅在导航属性仍然为空时设置数据库中的相关项。由于您在此处初始化了
症状
症状
.Include()
可能什么都不做


旁注:为什么将一个实体称为“症状”(单数),而将另一个实体称为“症状问题”(复数)?两者似乎只代表一个实例。

您只是查询数据,而不是更新数据。显示不符合您要求的代码。为了获得更多信息,您需要提供数据表图表。请编辑您的问题并提供表的相关部分。也给我们一些关系:校准是否有零个或多个症状?或者一个症状有几个阴蒂?对不起,伙计们。我将我的实体添加到问题中。
var calFile = await innerContext.Calibrate
    .Where(x => x.DataId == dataFile.Id)
    .Include(x => x.Symptom) 
        .ThenInclude(x => x.SymptomQuestions)
    .SingleOrDefaultAsync();