实体框架包含()不工作 [英] Entity Framework Include() is not working

查看:110
本文介绍了实体框架包含()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下EF查询:

  TestEntities db = new TestEntities(); 
var questions = from db in db.Questions.Include(QuestionType)
from sq in db.SurveyQuestions
其中sq.Survey == surveyTypeID
orderby sq.Order
选择q;

foreach(问题中的var问题){
//错误:空引用异常
Console.WriteLine(问题类型:+ question.QuestionType.Description);
}

当我访问QuestionType属性时,我得到一个空引用异常。我正在使用Include(QuestionType),但它似乎没有工作。我做错了什么?



编辑:当我打开Lazy Loading时,它不会引发null引用异常。



编辑:当我执行以下操作时,Include()似乎正常工作:

  var questions = db.Questions.Include(QuestionType)。选择(q => q); 

当我对单独的实体进行谓词包含似乎失败。使用Include时是不允许的?我的查询导致这个事情不起作用?

解决方案

问题可能与Linq表达式中的子查询有关。子选项,分组和投影可能会导致加载 Include 以静默方式失败,如这里,并更详细地解释这里(查看Diego Vega在线程中间某处的答案)。



虽然我不能真的看到你在这些帖子中使用 Include 时违反了任何规则,您可以尝试根据建议更改查询:

  var questions = from db in db.Questions 
from sq in db.SurveyQuestions
where sq.Survey == surveyTypeID
orderby sq.Order
选择q;

var questionsWithInclude =((ObjectQuery)问题).Include(QuestionType);

foreach(问题问题中的var问题WinterInclude){
Console.WriteLine(问题类型:+ question.QuestionType.Description);
}

(或使用帖子中提到的扩展方法。)



如果我正确理解链接的帖子,这并不一定意味着它现在可以工作(可能不会),但是你会得到一个例外,给你更多关于这个问题的细节。 p>

I have the following EF query:

TestEntities db = new TestEntities();
var questions = from q in db.Questions.Include("QuestionType")
                from sq in db.SurveyQuestions
                where sq.Survey == surveyTypeID
                orderby sq.Order
                select q;

foreach( var question in questions ) {
    // ERROR: Null Reference Exception
    Console.WriteLine("Question Type: " + question.QuestionType.Description);
}

I am getting a null reference exception when I access the QuestionType property. I am using Include("QuestionType") but it doesn't appear to be working. What am I doing wrong?

Edit: It does not throw a null reference exception when I have Lazy Loading turned on.

Edit: Include() seems to be working when i do the following:

var questions = db.Questions.Include("QuestionType").Select(q => q);

When I predicate on a separate entity Include seems to fail. Is that not allowed when using Include? What about my query is causing this thing to not work?

解决方案

The problem might be related to the subquery in your Linq expression. Subselects, grouping und projections can cause eager loading with Include to fail silently, as mentioned here and explained in more detail here (see answers of Diego Vega somewhere in the middle of the thread).

Although I cannot really see that you violate any of the rules to follow when using Include as described in those posts, you could try to change the query according to the recommendation:

var questions = from q in db.Questions
                from sq in db.SurveyQuestions
                where sq.Survey == surveyTypeID
                orderby sq.Order
                select q;

var questionsWithInclude = ((ObjectQuery)questions).Include("QuestionType");

foreach( var question in questionsWithInclude ) {
    Console.WriteLine("Question Type: " + question.QuestionType.Description);
}

(Or use the extension method mentioned in the posts.)

If I understand the linked posts correctly, this does not necessarily mean that it will work now (probably not), but you will get an exception giving you more details about the problem.

这篇关于实体框架包含()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆