【发布时间】:2020-04-06 21:46:57
【问题描述】:
要从多个级别加载数据,我使用 include,然后在我将项目更新到 dotnet core 3.1 后,它无法正常工作,第四级始终为空
var oldMethod = _depositRepository.All()
.Include(n => n.StudentStatus.Student.Person.Gender)
我尝试使用 ThenInclude
var newMethod = _depositRepository.All()
.Include(n => n.StudentStatus)
.ThenInclude(s => s.Student)
.ThenInclude(p => p.Person)
.ThenInclude(g => g.Gender)
在这两种方法中Gender 总是null
public IQueryable<T> All()
{
return DbSet.AsNoTracking().AsQueryable();
}
我按照这个教程Loading Related Data
编辑1:我的一些代码
public class IEntity<IId>
{
public IId Id { get; set; }
public string Name { get; set; }
}
public class Gender : IEntity<byte>
{
}
public class Person: IEntity<int>
{
public byte GenderId { get; set; }
public virtual Gender Gender { get; set; }
}
public class Student : IEntity<int>
{
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
public class StudentStatus : IEntity<int>
{
public int StudentId { get; set; }
public virtual Student Student { get; set; }
}
public class Deposit : IEntity<int>
{
public int StudentStatusId { get; set; }
public StudentStatus StudentStatus { get; set; }
}
public interface IRepository<T, IId>
where T : IEntity<IId>
{
IQueryable<T> All();
}
public class EfRepository<T, IId> : IRepository<T, IId>
where T : IEntity<IId>
{
private readonly UniversityDbContext _dbContext;
public EfRepository(UniversityDbContext dbContext)
{
_dbContext = dbContext;
}
private DbSet<T> DbSet => _dbContext.Set<T>();
public IQueryable<T> All()
{
return DbSet.AsNoTracking().AsQueryable();
}
public class FinancialController : Controller
{
private readonly IRepository<Deposit, int> _depositRepository;
public FinancialController(IRepository<Deposit, int>)
{
_depositRepository = depositRepository;
}
public IActionResult GetStatistic()
{
var model= _depositRepository.All()
.Include(n => n.StudentStatus)
.ThenInclude(s => s.Student)
.ThenInclude(p => p.Person)
.ThenInclude(g => g.Gender).ToList();
return Json(model);
}
}
【问题讨论】:
-
personRepository.All().Include(p => p.Gender)是否填充Gender? -
是的,它会加载数据。
-
抱歉,无法重现。你能提供一个minimal reproducible example吗?即使您在 EF Core GitHub 问题跟踪器上报告此问题,他们也会要求您这样做。
-
@IvanStoev 我更新问题
标签: c# entity-framework entity-framework-core