Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Include() is not working

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?

like image 444
Dismissile Avatar asked Dec 17 '10 21:12

Dismissile


People also ask

How you can load related entities in EF?

Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading. The techniques shown in this topic apply equally to models created with Code First and the EF Designer.

How to do eager loading?

Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by the use of the Include method. It means that requesting related data be returned along with query results from the database.

How do I use lazy loading in Entity Framework?

Lazy loading means delaying the loading of related data, until you specifically request for it. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

What is include in LINQ query C#?

Introduction to LINQ Include. LINQ include helps out to include the related entities which loaded from the database. It allows retrieving the similar entities to be read from database in a same query. LINQ Include() which point towards similar entities must read from the database to get in a single query.


1 Answers

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.

like image 174
Slauma Avatar answered Oct 04 '22 23:10

Slauma