【发布时间】: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<TEntity, TProperty>类型,它返回IQueryable<TEntity>类型,我们不能对其调用 ThenInclude。 -
这个在 EFD6 中工作,也可以在内核中工作 -Include("City") .Include("City.Province")
-
@Sergey,谢谢你的权利,我只是被文档误导了。
标签: asp.net-core entity-framework-core ef-core-5.0