2017-09-15 75 views
0

我想取的有儿孙EF核2.0 ThenInclude()导航不可达

实体实体下面的代码第一公约和如下

//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”集合,并留下了“实习课”属性为空预期。
问题出现时,我试图让孙子,通过使用

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

在那里,它仍然填充Operaciones,但在每个Operacion财产实习课停留空,我得到以下信息

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; 

这是一个错误?有什么办法可以避免使用嵌套选择?这些类非常大,因为它们有很多属性,并且使用该方法难以保留对域模型的更改。

谢谢。

编辑:编辑标题。

回答

0

嗯,我觉得这其实是一个错误。

此数据库在SQL Server 2016中运行,并通过Integration Services包从旧的,未维护的Visual Fox Pro数据库中迁移。

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

我认为这是一个错误,因为警告信息在我看来有点误导。它说我不能从Solicitud获得实际上是真实的Practica,因为它在数据库被破坏时永远不能得到我的实践,但是为什么我不能得到它们却不是很准确。

1

看来您需要启动查询from the entity you want as a result。在您的示例中Practica不存在于您的查询结果中,因为它是嵌套的(结果查询与Practica之间没有直接路径)。

你可以试着重写查询这种方式(如果不加导航属性里面Practica已经存在):

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

感谢您的回应! 不幸的是,这是不适合我的情况,因为Practica是具有参考标准化值的表格,而Operacion使用Practica来说明在某一天进行了哪种类型的外科手术。 我真的很想能够返回一个Solicitudes列表。 我也没有详细说明,因为“Practicas”是一个参考表,一个Operacion有一个Practica,但一个Practica有很多Operacions。所以 。这里(p => p.Operacion.Solicitud.Profesional == profesional) 失败,因为p.Operacion不是一个对象,而是一个ICollection。 –