title: Entity Framework 6 Code First Fluent API Table Mapping
body: <p>I have a table in an existing database that looks something like this:</p>
  
  <pre><code>PK
  FK
  Col1
  Col2
  Col3
  Col4
  Col5
  </code></pre>
  
  <p>I need to get it into a class hierarchy like this:</p>
  
  <pre><code>public abstract class BaseClass : Entity
  {
      public int PK {get; set;}
      public string Col1 {get; set;}
  }
  
  public class Child1 : BaseClass
  {
      public string Col2 {get; set;}
      public string Col3 {get; set;}
  }
  
  public class Child2 : BaseClass
  {
      public string Col4 {get; set;}
      public string Col5 {get; set;}
  }
  </code></pre>
  
  <p>I am currently using the Fluent API to configure the entities like so:</p>
  
  <pre><code>public abstract class BaseClassConfig&lt;TEntity&gt; : EntityTypeConfiguration&lt;TEntity&gt; where TEntity : Entity
  {
      public BaseClassConfig()
      {
          ToTable("TheTableName");
          HasKey(x =&gt; x.Id);
  
          Property(x =&gt; x.Col1).HasColumnName("SomeName");
      }
  }
  
  public class Child1Config : BaseClassConfig&lt;Child1&gt;
  {
      public Child1Config()
      {
          Property(x =&gt; x.Col2).HasColumnName("SomeName");
          Property(x =&gt; x.Col3).HasColumnName("SomeName");
      }
  }
  
  public class Child2Config : BaseClassConfig&lt;Child2&gt;
  {
      public Child2Config()
      {
          Property(x =&gt; x.Col4).HasColumnName("SomeName");
          Property(x =&gt; x.Col5).HasColumnName("SomeName");
      }
  }
  </code></pre>
  
  <p>When I added these to the context the inherits from <code>DbContext</code>:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Configurations.Add(new Child1Config());
      modelBuilder.Configurations.Add(new Child2Config());
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>The entity types 'Child1' and 'Child2' cannot share table 'TheTableName'
    because they are not in the same type hierarchy or do not have a valid
    one to one foreign key relationship with matching primary keys between
    them.</p>
  </blockquote>
  
  <p>I had a look at this article: <a href="http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application" rel="nofollow">http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application</a></p>
  
  <p>But it does not really talk about using the fluent api to configure the types, rather directly adding them to the context via <code>DbSet&lt;&gt;</code>.</p>
  
  <p>How do I setup a single table to map to different classes through a base class using the fluent api?</p>