0

我正在尝试在我的 LINQ 查询中添加来自对象父级的字段,但它不起作用。这是我的课程:

public Problem()
    {
        this.Questions = new List<Question>();
    }
    public int ProblemId { get; set; }
    public int SubTopicId { get; set; }
    public string Text { get; set; }
    public virtual SubTopic SubTopic { get; set; }
    public virtual ICollection<Question> Questions { get; set; }
}

public class Question : AuditableTable
{
    public int QuestionId { get; set; }
    public int ProblemId { get; set; }
    public virtual Problem Problem { get; set; }
    public virtual QuestionStatus QuestionStatus { get; set; }
}



 var questions = _questionsRepository
            .GetAll()
            .Include(q => q.Problem.SubTopicId)
            .Include(q => q.Answers)
            .ToList();

我收到此错误:

System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=A specified Include path is not valid. 
  The EntityType 'Models.Contexts.Problem' does not declare a navigation 
  property with the name 'SubTopicId'.
4

1 回答 1

3

您必须Include使用导航属性,而不是 ID:

.Include(x => x.Problem.SubTopic)

如果我是你,我会考虑从数据库中获取你真正需要的东西并返回一个映射对象,以减轻负载。而且你必须相当确定你不会在某个地方遇到 N+1 问题...... :)

于 2013-08-04T10:46:12.383 回答