title: Table Per Concrete Type (TPC) Inheritance in Entity Framework 6 (EF6)
body: <p>In an effort to avoid the use of Table Per Hierarchy (TPH) I have been looking at examples of how best to implement Table-Per-Concrete Class (TPC) inheritance in my database model. I came across the <a href="https://msdn.microsoft.com/en-us/data/jj591617#2.6" rel="noreferrer">official documentation</a> and <a href="https://datatellblog.wordpress.com/2015/04/12/inheritance-modelling-patterns-with-entity-framework-6-code-first/" rel="noreferrer">this article</a>.</p>
  
  <p>Below are some mock-up classes with some simple inheritance.</p>
  
  <pre><code>public class BaseEntity
  {
      public BaseEntity()
      {
          ModifiedDateTime = DateTime.Now;
      }
  
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      public DateTime ModifiedDateTime { get; set; }
  }
  
  public class Person : BaseEntity
  {
      public string FirstName { get; set; }
      public string LastName { get; set; }
  }
  
  public class Business :  BaseEntity
  {
      public string Name { get; set; }
      public string Location { get; set; }
  }
  </code></pre>
  
  <p>The DbModelBuilder configurations used per the examples in both articles.</p>
  
  <pre><code>modelBuilder.Entity&lt;BaseEntity&gt;() 
      .Property(c =&gt; c.Id) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 
  
  modelBuilder.Entity&lt;Person&gt;().Map(m =&gt; 
  { 
      m.MapInheritedProperties(); 
      m.ToTable("Person"); 
  }); 
  
  modelBuilder.Entity&lt;Business&gt;().Map(m =&gt; 
  { 
      m.MapInheritedProperties(); 
      m.ToTable("Business"); 
  });
  </code></pre>
  
  <p>The application runs successfully but when I go back to the database I find three (3) tables instead of the two (2) I expected to find. After a bit of testing it would appear the "BaseEntity" table is created but is never used. Everything seems to work just fine with the exception of this empty orphaned table.</p>
  
  <p>I mess around with the DbModelBuilder configurations, eventually removing the "BaseEntity" configurations which provides the expected result; Two (2) tables, each of them having the correct properties and functioning correctly.</p>
  
  <p>I do one last test, rip out all the DbModelBuilder configurations, only include the two (2) DbSet properties for "Person" and "Business" and test again.</p>
  
  <pre><code>public DbSet&lt;Person&gt; People { get; set; }
  public DbSet&lt;Business&gt; Businesses { get; set; }
  </code></pre>
  
  <p>To my surprise the project builds, goes out to the database, creates only the two tables with all the class properties including the inherited ones from the "BaseEntity" class. I can do CRUD operations without issue.</p>
  
  <p>After running many tests I can't find any issues with the final test and I have not been able to reproduce the duplicate key error both articles warned about.</p>
  
  <blockquote>
    <p>The changes to the database were committed successfully, but an error
    occurred while updating the object context. The ObjectContext might be
    in an inconsistent state. Inner exception message: AcceptChanges
    cannot continue because the object's key values conflict with another
    object in the ObjectStateManager. Make sure that the key values are
    unique before calling AcceptChanges.</p>
  </blockquote>
  
  <ol>
  <li>I am curious why the examples use the MapInheritedProperties property; is this an outdated method?</li>
  <li>Why do both examples say to include configuration properties for the "BaseEntity" yet including either the DbSet property or any DbModelBuilder configurations for the "BaseEntity" class causes an unused table to be created.</li>
  <li>In reference to the unique key error the articles warned of; I am unable to reproduce the error and I have tested many times with the primary key as either an int generated by the database and a guid generated by the database. Is the information about this error also obsolete or is there a test I can run to produce said error?</li>
  </ol>