Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Include() with inherited entities
    primarykey
    data
    text
    <p>In EF eager loading related entities is <a href="https://stackoverflow.com/questions/2092730/entitiy-framework-object-tree-loading">easy</a>.</p> <p>But I'm having difficulties <strong>including inherited entities</strong> when loading data using table-per-type model.</p> <p>This is my model:</p> <p><strong>Entities</strong>:</p> <ul> <li><code>ArticleBase</code> (base article entity) <ul> <li><code>ArticleSpecial</code> (inherited from <code>ArticleBase</code>)</li> </ul></li> <li><code>UserBase</code> (base user entity) <ul> <li><code>UserSpecial</code> (inherited from <code>UserBase</code>)</li> </ul></li> <li><code>Image</code></li> </ul> <p><strong>Relations</strong> are as shown on the image (omitting many columns): <a href="http://i48.tinypic.com/5x4kdc.jpg" rel="nofollow noreferrer">alt text http://i48.tinypic.com/5x4kdc.jpg</a></p> <p>In reality my users are always of type <code>UserSpecial</code>, since <code>UserBase</code> is used in another application, thus we can share credentials. That's the only reason I have two separate tables. <code>UserBase</code> table can't be changed in any way shape or form, because the other app would break.</p> <h2>Question</h2> <p><strong>How am I suppose to load <code>ArticleSpecial</code> with both <code>CreatedBy</code> and <code>EditedBy</code> set, so that both are of type <code>UserSpecial</code> (that defines <code>Image</code> relation)?</strong></p> <hr> <p>I've tried (unsuccessfully though) these options:</p> <p>1. <strong>Using lambda expressions:</strong></p> <pre><code>context.ArticleBases .OfType&lt;ArticleSpecial&gt;() .Include("UserCreated.Image") .Include("UserEdited.Image"); </code></pre> <p>In this case the problem is that both <code>CreatedBy</code> and <code>EditedBy</code> are related to <code>UserBase</code>, that doesn't define <code>Image</code> navigation. So I should somehow cast these two to <code>UserSpecial</code> type like:</p> <pre><code>context.ArticleBases .OfType&lt;ArticleSpecial&gt;() .Include("UserCreated&lt;UserSpecial&gt;.Image") .Include("UserEdited&lt;UserSpecial&gt;.Image"); </code></pre> <p>But of course using generics in <code>Include("UserCreated&lt;UserSpecial&gt;.Image")</code> don't work.</p> <p>2. <strong>I have tried using LINQ query</strong></p> <pre><code>var results = from articleSpecial in ctx.ArticleBase.OfType&lt;ArticleSpecial&gt;() join created in ctx.UserBase.OfType&lt;UserSpecial&gt;().Include("Image") on articleSpecial.UserCreated.Id equals created.Id join edited in ctx.UserBase.OfType&lt;UserSpecial&gt;().Include("Image") on articleSpecial.UserEdited.Id equals edited.Id select articleSpecial; </code></pre> <p>In this case I'm only getting <code>ArticleSpecial</code> object instances without related properties being set. I know I should select those somehow, but I don't know how?</p> <p>Select part in my LINQ could be changed to something like</p> <pre><code>select new { articleSpecial, articleSpecial.UserCreated, articleSpecial.UserEdited }; </code></pre> <p>but images are still not loaded into my context. My joins in this case are barely used to filter out articleSpecial results, but they don't load entities into context (I suppose).</p>
    singulars
    1. This table or related slice is empty.
    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