여러 수준에서 데이터를로드하려면 내 프로젝트를 dotnet core 3.1로 업데이트 한 후 include를 사용하면 작동하지 않으며 네 번째 수준은 항상 null입니다.
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();
}
이 튜토리얼을 따릅니다. 관련 데이터로드
편집 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);
}
}
나는 당신 ThenInclude
이 3 단계 로 얼마나 깊이 갈 수 있는지에 대한 제한이 있다고 결론을 내립니다 . 그래서 나는 객체 를 얻기 위해 호출 _genderRepository
하고 전달 GenderId
하여 문제를 Gender
해결하지만 이것이 최선의 해결책이라고 생각하지 않지만 sql
코드를 작성하는 것보다 쉽습니다 .
var newMethod = _depositRepository.All()
.Include(n => n.StudentStatus)
.ThenInclude(s => s.Student)
.ThenInclude(p => p.Person)
.ThenInclude(g => g.Gender)
.Select(n=>new {
...
Gender=_genderRepository.Find(k=>k.Id==n.StudentStatus.Student.Person.GenderId)
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다