简体   繁体   English

Ef核心LazyLoading - 类型集合的访问嵌套导航属性抛出DetachedLazyLoadingWarning错误

[英]Ef core LazyLoading - Access nested navigation property of type collection threw DetachedLazyLoadingWarning error

I tried to access the GradeInfo property of the student's latest grade using ef core 2.1 我尝试使用ef core 2.1访问学生最新成绩的GradeInfo属性

I listed the models at the end of the question 我在问题的最后列出了模型

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

In addition I use lazy loading proxies (from Microsoft.EntityFrameworkCore.Proxies) in startup.cs 另外我在startup.cs中使用延迟加载代理(来自Microsoft.EntityFrameworkCore.Proxies)

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

The error which is thrown is: 抛出的错误是:

"Error generated for warning Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning: An attempt was made to lazy-load navigation property 'Info' on detached entity of type 'GradeProxy'. Lazy-Loading is not supported for detached entities or entities that are loaded with 'AsNoTracking()'." “为警告Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning生成错误:尝试在”GradeProxy“类型的分离实体上延迟加载导航属性”Info“。对于已加载的分离实体或实体,不支持Lazy-Loading 'AsNoTracking()'“。

In addition, i want to state that I tried to add Inculde to the sudents's dbset as described in the following code, but the problem did not resolved. 另外,我想声明我尝试将Inculde添加到sudents的dbset中,如下面的代码所述,但问题没有解决。

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

models 楷模

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

For this issue, it is caused by that if you change the query so that it no longer returns instances of the entity type that the query began with, then the include operators are ignored. 对于此问题,它是由于如果您更改查询以使其不再返回查询开头的实体类型的实例,则会忽略包含运算符。 You could refer Ignored includes . 你可以参考Ignored includes

And, currently, it is not supported to query the navigation property by Include , refer Are .Include/.ThenInclude supposed to work with Select in RC1-final? 并且,目前,不支持通过Include查询导航属性,请参阅.Include / .ThenInclude应该与RC1-final中的Select一起使用? #4641 . #4641

For a workaround, you need to query all the columns in the db and then query the expected type at client side. 要解决此问题,您需要查询db中的所有列,然后在客户端查询预期的类型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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