我试图通过此查询急切加载相关集合的第一条记录:
public async Task<IEnumerable<SparePart>> GetFront()
{
return await _db.SparePart.Include(x => x.Photos.FirstOrDefault()).OrderBy(x => x.Price).Take(12).AsNoTracking().ToArrayAsync();
}
但我收到错误The Include path expression must reference the navigation property defined in the type
这对我来说很奇怪,因为我在相关实体中有这个导航属性:
public partial class Photo
{
...
public int SparePartId { get; set; }
[ForeignKey("SparePartId")]
public virtual SparePart SparePart { get; set; }
}
与父实体相同:
公共部分类SparePart
{
public SparePart()
{
Photos = new HashSet<Photo>();
}
...
public virtual ICollection<Photo> Photos { get; set; }
...
}