簡體   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