1

我試圖訪問我的控制器的詳細信息操作中我的主模型集合的集合。但我總是收到以下錯誤實體框架訪問集合

System.InvalidOperationException:'屬性表達式'e => {來自[e] .Colors中的顏色顏色選擇[color] .Images}'無效。表達式應該表示一個屬性訪問:'t => t.MyProperty'。有關包括相關數據的更多信息,請參見http://go.microsoft.com/fwlink/?LinkID=746393。'

這個彈出上線:

var model = _context.Towers 
    .Include(e => e.Colors.Select(color => color.Images)) 
    .FirstOrDefault(e => e.ID == id); 

下面是一些其他代碼:

Tower.cs

public class Tower 
{ 
    [Key] 
    public Nullable<int> ID { get; set; } 
    public List<Color> Colors { get; set; } = new List<Color>(); 
} 

Color.cs

public class Color 
{ 
    [Key] 
    public Nullable<int> ID { get; set; } 
    public string ColorHash { get; set; } 
    public List<Image> Images { get; set; } = new List<Image>(); 
} 

Image.cs

public class Image 
{ 
    [Key] 
    public Nullable<int> ID { get; set; } 
    public string ImagePath { get; set; } 
} 

我需要能夠訪問圖片每個顏色我展示相關的關聯的詳細信息。

回答

3

我想應該是這樣的:

var model = _context.Towers 
    .Include(e => e.Colors) 
     .ThenInclude(color => color.Images)) 
    .FirstOrDefault(e => e.ID == id); 
+0

是啊。就是這樣。非常感謝 –