6

Ok, so according to this page, the Entity Framework should eagerly load multiple levels by using a Select within the Include method.

I have the following code:

var reports = _context.Reports
    .Include(rt => rt.Fields)
    .Include(rt => rt.Fields.Select(f => f.FieldType))
    .Include(rt => rt.Fields.Select(f => f.FieldType.FieldIdentifier));

Yet this throws an InvalidOperationException - "Invalid type of expression" on the call to the second include. The Exception is coming from EntityFrameworkHelper.CollectRelationalMemebers.

I also tried using strings to Include related properties, but that failed as well (I'd rather avoid using the strings if at all possible).

I'm using the EF 5.0 DLL for .NET 4.0. My EF classes are old-fashioned database-first EntityObjects.

Does anyone know the cause and if there's anything I can do about this exception?

EDIT:

When using the string version:

var reports = _context.Reports
    .Include("Fields")
    .Include("Fields.FieldType")
    .Include("Fields.FieldType.FieldIdentifier"));

It throws InvalidOperationException - Invalid type of Expression.

4
  • What is the exact exception message when you use the string-based version of Include? Commented Feb 20, 2013 at 20:00
  • I meant the exception for the string based version of Include. Commented Feb 20, 2013 at 20:39
  • Sorry, that was dumb. I just had pasted the wrong code into the second example. That is the exception for the string based version. Commented Feb 20, 2013 at 20:43
  • The exception is really "Invalid type of expression" for the string version? That's strange because a string is not an expression. Commented Feb 20, 2013 at 20:50

2 Answers 2

21

You have redundant includes. You only need the last include, which will include anything in that path. Example:

var reports = _context.Reports
    .Include(rt => rt.Fields.Select(f => f.FieldType.FieldIdentifier));
Sign up to request clarification or add additional context in comments.

Comments

0

The issue ended up being that a 3rd party Include extension method in a DLL we had be using was getting precedence over the Entity Framework's Include. The 3rd party one was throwing the exceptions. After removing it the issue is resolved.

One of the problems with extension methods...

Thanks for the help everyone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.