Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with Eager Loading Nested Navigation Based on Abstract Entity (EF CTP5)
    primarykey
    data
    text
    <p>I a portion of my EF model that looks like this:</p> <p><img src="https://i.stack.imgur.com/XGvyH.png" alt="enter image description here"></p> <p>Summary:</p> <ul> <li>Location has <strong>many</strong> Posts</li> <li>Post is an <strong>abstract</strong> class</li> <li>Discussion <strong>derives</strong> from <strong>Post</strong></li> <li>Discussions have <strong>many</strong> Comments</li> </ul> <p>Now, the query i'm trying to achieve:</p> <p><strong>Get information about Location Id 1234, including any Discussions and Comments associated with those Discussions.</strong></p> <p>I can get discussions and the comments like this:</p> <pre><code>var discussions = ctx.Posts .OfType&lt;Discussion&gt;() .Include(x =&gt; x.Comments) .ToList(); </code></pre> <p>But i can't seem to get it based on the <strong>Posts</strong> navigation on the <strong>Location</strong> entity.</p> <p>I've tried this:</p> <pre><code>var locationWithDiscussionsAndComments = ctx .Locations .Include(x =&gt; x.Posts .OfType&lt;Discussion&gt;() .Select(y =&gt; y.Comments)) .SingleOrDefault(); </code></pre> <p>Which compiles, but i get the error:</p> <blockquote> <p>System.ArgumentException: The include path expression must refer to a property defined by the entity, optionally also with nested properties or calls to Select. Parameter name: path</p> </blockquote> <p>Any ideas? I could probably go "backwards" from the Posts:</p> <pre><code>var locationWithDiscussionsAndComments = ctx .Posts .Include(x =&gt; x.Location) .OfType&lt;Discussion&gt;() .Include(x =&gt; x.Comments) .Where(x =&gt; x.LocationId == 1234) .Select(x =&gt; x.Location) .ToList(); </code></pre> <p>But that is both hairy and semantically wrong in terms of my repositories (i shouldn't have to go through a post repository to get information about a location).</p> <p>Any ideas?</p> <p><strong>EDIT</strong></p> <p>So after having a bigger think about it, i realized that <code>OfType&lt;T&gt;</code> is a filter operation. As as we know, EF does not support filtering with eager loading. The only options are retrieving <strong>everything</strong>, or using anonymous type projection.</p> <p>No way i can retrieve everything, as there is far too much meta data involved. So i'm attempting the anonymous type projection.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload