EF Core 2.0 ThenInclude() navigation not reachable

c# entity-framework entity-framework-core


I'm trying to fetch entities which have children and grandchildren

The Entities are following code first conventions and are as follows

//This is the father class
public partial Class 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
    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

If I do

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

It works out ok, populating the "Operaciones" collections, and leaving the "Practica" property as null as expected.
The problem arises when I try to get the grandchildren, by use of

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

There, it still populates Operaciones, but in each Operacion the property practica stays null, and I get the following message

    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.

Which to me makes no sense because I could very well do

String something = solicitud.Operaciones.ElementAt(0).Practica.Nombre;

Is this a bug? Is there any way I can avoid using nested selects? The classes are really big in that they have a lot of Properties and it becomes difficult to mantain changes to the domain model using that approach.


Edit: edited title.

9/15/2017 4:02:06 PM

Accepted Answer

Well, I think this is actually a bug.

This database is running in SQL Server 2016 and migrated from an old, kind of unmaintained Visual Fox Pro database via an Integration Services package.

Somehow something went wrong while writing said package and the database ended up with rows that broke a foreign key restriction (specifically the one relating Operaciones to Practicas), once I deleted the rows that broke the restriction I ran the query again and it successfully populated every member it was supposed to.

I think this is a bug because the warning message was, in my opinion, a bit misleading. It said that I couldn't access Practica from Solicitud which is tecnically true because it could never get me any Practica as the database was broken, but not very accurate in why I couldn't get them.

9/18/2017 1:32:32 PM

Popular Answer

It seems that you need to start the query from the entity you want as a result. In your example Practica is not present in the result of your query because is nested (there is no direct path between your resulting query and Practica).

You could try to rewrite your query this way (and add a navigation property inside Practica if not already present):

    .Include(p => p.Operacion)
    .ThenInclude(o => o.Solicitud)
    .Where(p => p.Operacion.Solicitud.Profesional == profesional)

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow