Question

Thank you for your thoughts

I am using entity framework 5.0, model first.

a table is mapped using a table per class (or table per concrete type) pattern.

Table A 1..Many (Abstract) Table B

Table B has 2 child classes

B1 & B2

B2 has a foreign key to a 3rd table, let's say Table C (Many B2 ... 1 C), but this is not a property of the parent class B.

Eager loading is the default for the application, and I want to include Table (or collection) C when querying on table B2 - the equivalent of (pseudo linq to entities):

from A in _db.A
.Include(A=>A.B.OfType<B2>())
.Include(A=>A.B.OfType<B2>().Include(C))
select A

thanks for any thoughts for how I can force an eager load of this table

Was it helpful?

Solution

The heart of the problem is that Include(A=>A.B.OfType<B2>()) is not supported.

As far as I understand the ADO.Net team it seems that they don't want to support partially loaded child collections. (And I tend to agree with them.) In Linq-to-sql there were DataLoadOptions, but there is no EF equivalent of them. That could explain why Include with OfType is not supported either, because it would tell EF to partly load the collection of Bs.

Moreover, the extension method Include is only a wrapper around the Include method (if present) of an IQueryable objects itself. Take ObjectQuery.Include: a pretty simple method with just a string as parameter (Same for DbQuery.Include) that contains the path to the navigation property to be included (_db.A.Include("B")). In other words: you can only only use strings that can be resolved to a MemberExpression. And B.OfType<>() is a method.

Long story to explain why you can't include C: the path leading to C is not valid.

You could do

_db.B.OfType<B2>().Include(b => b.A).Include(b => b.C)

But that may not give you the results you were after.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top