【问题标题】:Entity Framework Core eager loading by navigation name(string) [duplicate]Entity Framework Core通过导航名称(字符串)预先加载[重复]
【发布时间】:2021-01-18 22:25:04
【问题描述】:

我有 2 个实体类如下:

public class Office
{
    public int Id { get; set; }
    public string Address { get; set; }
    public string Telephone { get; set; }
    public int CityId { get; set; }

    public City City { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ProvinceId { get; set; }

    public Province Province { get; set; }
}

我知道我可以通过以下方式加入 City:

IQueryable<Office> offices = _context.Offices.Include(o => o.City);

//or

IQueryable<Office> offices = _context.Offices.Include("City");        

并包括城市和省:

IQueryable<Office> offices = _context.Offices.Include(o => o.City).ThenInclude(c=>c.Province);

我的问题是,有没有办法使用字符串参数来包含省?类似Include("City.Province")ThenInclude("Province")

【问题讨论】:

  • 你可以使用 Include("City") .ThenInclude("Province")。
  • @Sergey ,不幸的是没有ThenInclude() 重载将参数作为字符串。而Include(string nav) 不返回IIncludableQueryable&lt;TEntity, TProperty&gt; 类型,它返回IQueryable&lt;TEntity&gt; 类型,我们不能对其调用 ThenInclude。
  • 这个在 EFD6 中工作,也可以在内核中工作 -Include("City") .Include("City.Province")
  • @Sergey,谢谢你的权利,我只是被文档误导了。

标签: asp.net-core entity-framework-core ef-core-5.0


【解决方案1】:

只需使用:

Include("City").Include("City.Province")

【讨论】:

    猜你喜欢
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 2020-06-03
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多