更新时间:2023-02-13 12:36:23
Question
表,因为FK的shadow属性仅在Answer对象的关系的很多侧创建。
There shouldn't be any FK in the Question
table, because the shadow property for the FK is created only on the 'many' side of the relationship in the Answer object.
基本上,您有两个选项:
显式加载答案:
Explicitly load the Answers:
var questions = _context.Question.OrderByDescending(x => x.AddDate).Take(10);
_contest.Answer.Where(x => questions.Select(q => q.Id)
.Contains(EF.Property<int>(x, "QuestionId"))).Load();
在这有点令人费解的陈述之后,所有答案将填充适当的对象。请注意FK使用自动创建的shadow属性。 此处。
After this a bit convoluted statement all Answers will be populated with the appropriate objects. Note the use of the autocreated shadow property for FK. More on the naming conventions here.
更容易渴望加载集合:
Far more easily would be to eagerly load the collections:
_context.Question.OrderByDescending(x => x.AddDate).Take(10).Include(x => x.Answers);
这效率更高,可读性强
另外,值得注意的是, 目前,EF Core不支持延迟加载 。
Also, it's worth noting that EF Core does not support lazy loading as of this moment.