[英]EF Core avoid loading Parent entity when using Include
I have the following code我有以下代码
public class Question
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Required]
public string QuestionText { get; set; }
public virtual ICollection<QuestionOption> QuestionOptions { get; set; }
}
public class QuestionOption
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Required]
public string OptionText { get; set; }
[ForeignKey("QuestionId")]
public Question Question { get; set; }
public long QuestionId { get; set; }
public virtual ICollection<QuestionOptionAnswer> QuestionOptionAnswers { get; set; }
}
public class QuestionOptionAnswer
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[ForeignKey("QuestionOptionId")]
public QuestionOption QuestionOption { get; set; }
public long QuestionOptionId { get; set; }
}
and the following query to retrieve the question:和以下查询来检索问题:
var dbResult = await (from question in context.Question.Include(x => x.QuestionOptions).ThenInclude(x => x.QuestionOptionAnswers)
where question.Id == id
select question).FirstOrDefaultAsync();
Everything works fine and the query returns what I really need except from the fact that I can see in a QuestionOption the parent Entity(Question) which I already have.一切正常,查询返回我真正需要的东西,除了我可以在 QuestionOption 中看到我已经拥有的父 Entity(Question)。 I have cases that I have 10 question options for a question and each one has the Question parent entity returned.
我有一些情况,我有 10 个问题选项,每个问题都返回了 Question 父实体。 This is not desired as I am getting the same thing I already have multiple times.
这是不希望的,因为我已经多次得到同样的东西。 How can I prevent this from happening?
我怎样才能防止这种情况发生?
You aren't getting the parent object multiple times, each parent is returned as a single row from the database and one object is created for each Question
.您没有多次获取父对象,每个父对象都作为一行从数据库中返回,并且为每个
Question
创建一个对象。
It is referenced in multiple places though, including in your QuestionOption
, which as little impact on performance.它在多个地方被引用,包括在您的
QuestionOption
中,这对性能影响很小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.