【问题标题】:Load Related Data entity ef Core 3.1 using in Include使用包含加载相关数据实体 ef Core 3.1
【发布时间】: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 =&gt; p.Gender) 是否填充 Gender
  • 是的,它会加载数据。
  • 抱歉,无法重现。你能提供一个minimal reproducible example吗?即使您在 EF Core GitHub 问题跟踪器上报告此问题,他们也会要求您这样做。
  • @IvanStoev 我更新问题

标签: c# entity-framework entity-framework-core


【解决方案1】:

我的结论是,ThenInclude 的深度有一个限制,即三个级别。所以我通过调用_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)
                                     }

【讨论】:

  • 嗨,我最近使用了 .net core 3.1,我们使用 ThenInclude 深入了 4 个级别,没有任何问题。所以我猜你的代码/数据中还有其他问题。您是否尝试过获取具有相同结果的不同属性?另外,数据中是否存在带有id的性别?
  • 是的,我尝试获取 Department 对象并获取部门的 null
猜你喜欢
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多