繁体   English   中英

EF Core 2.0 ThenInclude()导航无法访问

[英]EF Core 2.0 ThenInclude() navigation not reachable

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

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

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

如果我做

context.Solicitudes
            .Include(w => w.Operaciones)
            .Where(x => x.Profesional == profesional).OrderBy(something);

可以正常工作,填充“ Operaciones”集合,并按预期将“ Practica”属性保留为空。
当我尝试通过使用

context.Solicitudes
            .Include(w => w.Operaciones)
                .ThenInclude(o => o.Practica)
            .Where(x => x.Profesional == profesional);

在那里,它仍然填充Operaciones,但是在每个Operacion中,属性practica保持为空,并且我得到以下消息

    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();

好吧,我认为这实际上是一个错误。

该数据库在SQL Server 2016中运行,并通过Integration Services程序包从一种旧的,未维护的Visual Fox Pro数据库进行了迁移。

不知何故,写上述包时出了问题,数据库的行尾打破了外键限制(特别是将Operaciones与Practicas相关的行),一旦我删除了打破限制的行,我再次运行了查询,并成功填充了每个应该的成员。

我认为这是一个错误,因为我认为警告消息有些误导。 它说我无法从Solicitud访问Practica,这在技术上是正确的,因为在数据库损坏时它永远无法获得任何Practica,但对于为什么我无法获得它们却不是很准确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM