2017-09-15 27 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。 –