title: Entity Framework 6 - inheritance and navigation properties on base class
body: <p>I have a problem with navigation properties and inheritance.</p>
  
  <p>This is my problem:
  I have a base <code>Person</code> class and classes <code>User</code> and <code>Worker</code> which inherit from <code>Person</code>. On the DB level I'm using single table inheritance or table per hierarchy (TPH) inheritance. So there a single table with a discriminator column.</p>
  
  <p>Both <code>User</code> and <code>Worker</code> need to have a <code>Company</code> relation, so I would like to define it on the <code>Person</code> class.</p>
  
  <p>I define my model like this:</p>
  
  <pre><code>[Table("mydb.person")]
  public abstract partial class Person
  {
      [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public long ID { get; set; }
  
      public long? CompanyID { get; set; }
      [ForeignKey("CompanyID")]
      public virtual Company Company { get; set; }
      ...
  }
  
  public partial class User : Person
  {
      ...
  }
  
  public partial class Worker : Person
  {
      ....
  }
  
  [Table("mydb.company")]
  public partial class Company
  {
      public Company()
      {
          this.People = new HashSet&lt;Person&gt;();
          this.Users = new HashSet&lt;User&gt;();
          this.Workers = new HashSet&lt;Worker&gt;();
      }
  
      [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public long ID { get; set; }
  
      public virtual ICollection&lt;Person&gt; People { get; set; }
  
      public virtual ICollection&lt;User&gt; Users { get; set; }
  
      public virtual ICollection&lt;Worker&gt; Workers { get; set; }
  
      ...
  }
  </code></pre>
  
  <p>Now, when I try to do a query to get the user and related company, for example:</p>
  
  <pre><code>dbSet.Where(u =&gt; u.Username == username).Include(x =&gt; x.Company).FirstOrDefault();
  </code></pre>
  
  <p>The query fails with this exception:</p>
  
  <blockquote>
    <p>Unknown column 'Extent1.Company_ID' in 'field list</p>
  </blockquote>
  
  <p>If I examine the result SQL it looks something like this:</p>
  
  <pre><code>SELECT
  1 AS `C1`, 
  @gp2 AS `C2`, 
  `Extent1`.`ID`, 
  `Extent1`.`CompanyID`, 
  `Extent1`.`Username`,
  ...
  `Extent1`.`Company_ID`
  FROM `person` AS `Extent1`
   WHERE `Extent1`.`Discriminator` = @gp1 
  </code></pre>
  
  <p>It includes the extra <code>Company_ID</code> column, which doesn't exist.</p>
  
  <p>I tried a few thing, nothing worked out:</p>
  
  <ul>
  <li>renaming the column from <code>CompanyID</code> to <code>Company_ID</code> -> it generates a <code>Column_ID1</code> in SQL and throws the same exception</li>
  <li>removing the <code>Users</code> and <code>Workers</code> relations from <code>Company</code> -> it throws an exception saying it doesn't know how to map <code>User</code> and <code>Company</code> entities:</li>
  </ul>
  
  <blockquote>
    <p>Unable to determine the principal end of an association between the
    types 'Models.User' and 'Models.Company'. The principal end of this
    association must be explicitly configured using either the
    relationship fluent API or data annotations.</p>
  </blockquote>
  
  <ul>
  <li>If I remove all 3 navigation properties from <code>Company</code> it throws the same mapping exception as above</li>
  </ul>
  
  <p>I'm out of "clean" ideas at the moment. The only thing that could work is to do some dirty hack, define all the relations on child classes, and do separate queries and merging in the base class if both users and workers are required.</p>
  
  <p>Do you have any suggestions?</p>