简体   繁体   中英

Load Related Data entity ef Core 3.1 using in Include

To load data from multiple level I use include then after I update my project to dotnet core 3.1 it's not working and fourth level always null

var oldMethod = _depositRepository.All()
                    .Include(n => n.StudentStatus.Student.Person.Gender)

and I try using ThenInclude

var newMethod = _depositRepository.All()
                    .Include(n => n.StudentStatus)
                        .ThenInclude(s => s.Student)
                            .ThenInclude(p => p.Person)
                                .ThenInclude(g => g.Gender)

In both method the Gender always be null

public IQueryable<T> All()
{
    return DbSet.AsNoTracking().AsQueryable();
}

I follow this tutorial Loading Related Data

Edit 1: some of my code

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

调试窗口

I conclude that there is a restriction on how deep you can go with ThenInclude which is three level. So I work around it by calling _genderRepository and pass GenderId to get Gender object however I don't think this is the best solution but it's easier than write sql code.

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)
                                     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM