实体框架访问集合
问题描述:
我试图访问我的控制器的详细信息操作中我的主模型集合的集合。但我总是收到以下错误实体框架访问集合
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; }
}
我需要能够访问图片每个颜色与塔我展示相关的关联的详细信息。
答
我想应该是这样的:
var model = _context.Towers
.Include(e => e.Colors)
.ThenInclude(color => color.Images))
.FirstOrDefault(e => e.ID == id);
是啊。就是这样。非常感谢 –