EF核心LazyLoading-类型集合的访问嵌套导航属性引发DetachedLazyLoadingWarning错误

时间:2019-02-26 13:29:57

标签: c# entity-framework linq asp.net-core .net-core

我尝试使用ef core 2.1访问该学生最新成绩的GradeInfo属性

我在问题末尾列出了模型

var students = applicationDbContext.Students.Where(s => s.Id ==2)
    .Select(student => new { 
        LatestGrade = student.Grades.OrderBy(g => g.Date).FirstOrDefault(),
        Id = student.Id
        }).ToList();

此外,我在startup.cs中使用延迟加载代理(来自Microsoft.EntityFrameworkCore.Proxies)

services.AddDbContext<ApplicationDbContext>(options => 
    options.UseLazyLoadingProxies()
           .UseSqlServer(connectionString));

引发的错误是:

  

“为警告Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning而生成的错误:尝试对类型为'GradeProxy'的分离实体延迟加载导航属性'Info'。分离实体或以下实体不支持延迟加载加载了'AsNoTracking()'。”

此外,我想说明一下,我尝试按照以下代码中的说明将Inculde添加到用户的dbset中,但是问题没有解决。

var student = applicationDbContext.Students.Include( s => s.Grades )
                .ThenInclude( grade => grade.Info).Where(...).Select(...)

模型

class Student{
   public int Id { get; set;}
   public virtual ICollection<Grade> Grades { get; set;}

   ...
}

class Grade {
   public virtual GrandeInfo Info { get; set;}
   public DateTime Date { get; set;}
}

class GrandeInfo {
  public int Id { get; set;}
  public int Score { get; set;}
}

1 个答案:

答案 0 :(得分:2)

对于此问题,其原因是,如果更改查询以使其不再返回查询开始的实体类型的实例,则将忽略include运算符。您可以参考Ignored includes

并且,当前不支持通过Include查询导航属性,请参考Are .Include/.ThenInclude supposed to work with Select in RC1-final? #4641

要解决此问题,您需要查询数据库中的所有列,然后在客户端查询所需的类型。

var students = _context.Students
                    .Where(s => s.Id == 2)
                    .ToList()
                    .Select(student => new
                    {
                        LatestGrade = student.Grades.OrderBy(g => g.Date).FirstOrDefault(),
                        Id = student.Id
                    })
                    .ToList();
相关问题