EF Eager loading。如何检索相关集合的第一个元素?

时间:2017-03-17 12:50:01

标签: c# entity-framework eager-loading

我试图通过此查询急切加载相关集合的第一条记录:

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; }
    ...
}

0 个答案:

没有答案