Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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# EF Core 2.0 ThenClude()导航不可访问_C#_Entity Framework_Entity Framework Core - Fatal编程技术网

C# EF Core 2.0 ThenClude()导航不可访问

C# EF Core 2.0 ThenClude()导航不可访问,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我正在尝试获取有孩子和孙子的实体 实体遵循代码优先约定,如下所示 //This is the father class public partial Class Solicitud{ [InverseProperty("Solicitud")] public virtual ICollection<Operacion> Operaciones { get; set; } //Other properties } //This is the child c

我正在尝试获取有孩子和孙子的实体

实体遵循代码优先约定,如下所示

//This is the father class
public partial Class Solicitud{
    [InverseProperty("Solicitud")]
    public virtual ICollection<Operacion> Operaciones { get; set; }  
    //Other properties
}

//This is the child class
public partial Class Operacion{
    [JsonIgnore] //This is so when serializing we don't get a circular reference
    [InverseProperty("Operaciones")]
    public virtual Solicitud Solicitud { get; set; }
    public virtual Practica Practica { get; set; }
    //Other Properties
}

//This is the grandchild class
public partial Class Practica
{
    String Nombre;
    //Other Properties
}
它工作正常,填充Operaciones集合,并按预期将Practica属性保留为null。 当我试图通过使用计算机来获得孙子孙女时,问题就出现了

context.Solicitudes
            .Include(w => w.Operaciones)
                .ThenInclude(o => o.Practica)
            .Where(x => x.Profesional == profesional);
在那里,它仍然填充Operaciones,但在每个Operacion中,属性practica保持为null,我得到以下消息

    warn: Microsoft.EntityFrameworkCore.Query[100106]
  The Include operation for navigation '[w].Operaciones.Practica' is unnecessary and was ignored because the navigation is not reachable in the final query results. See https://go.microsoft.com/fwlink/?linkid=850303 for more information.
这对我来说毫无意义,因为我可以做得很好

String something = solicitud.Operaciones.ElementAt(0).Practica.Nombre;
这是虫子吗?有什么方法可以避免使用嵌套选择吗?这些类非常大,因为它们有很多属性,使用这种方法很难对域模型进行更改

谢谢


编辑:编辑标题。

您似乎需要开始查询。在您的示例中,查询结果中不存在Practica,因为结果查询和Practica之间没有直接路径

您可以尝试以这种方式重写查询,并在Practica中添加导航属性(如果尚未存在):

context.Practicas
    .Include(p => p.Operacion)
    .ThenInclude(o => o.Solicitud)
    .Where(p => p.Operacion.Solicitud.Profesional == profesional)
    .ToList();

嗯,我认为这实际上是一个bug

该数据库在SQL Server 2016中运行,并通过集成服务包从一个旧的、未维护的Visual Fox Pro数据库迁移而来

不知何故,在编写所述包时出现了一些错误,数据库最终出现了违反外键限制的行,特别是与Operaciones和Practicas相关的行。一旦我删除了违反限制的行,我再次运行查询,它成功地填充了它应该填充的每个成员


我认为这是一个错误,因为在我看来,警告信息有点误导。它说我无法从Clarcud访问Practica,这在技术上是正确的,因为它无法为我获取任何Practica,因为数据库已损坏,但我无法获取它们的原因并不十分准确。

感谢您的回复!不幸的是,对于我的案例来说,这是不可行的,因为Practica是一个具有参考标准化值的表,Operacion使用Practica来说明在某一天进行了哪种类型的外科手术。我真的很想能寄一份招揽名单。我也没有详细说明,因为Practicas是一个参考表,一个操作有一个Practica,但一个Practica有许多操作。因此.Wherep=>p.Operacion.requestud.professional==professional失败,因为p.Operacion不是单个对象,而是一个ICollection。
context.Practicas
    .Include(p => p.Operacion)
    .ThenInclude(o => o.Solicitud)
    .Where(p => p.Operacion.Solicitud.Profesional == profesional)
    .ToList();