title: How can I mix TPH and TPT in Entity Framework 6?
body: <h2>Initial situation</h2>
  
  <p>I have an application that uses an existing database, currently using <strong>NHibernate</strong> as O/R-Mapper.<br>
  Now I need to migrate to <strong>Entity Framework 6.1.1</strong> using <strong>Code First</strong> and <strong>Fluent API Configuration</strong>.</p>
  
  <p>But now I have a problem with a part of the data model because it uses different types of inheritance strategies (TPT and TPH)</p>
  
  <h2>Structure</h2>
  
  <p><em>Note: Posting the complete data model here seemed a bit too enormous to me so I reproduced the problem I face in a small POC program.</em></p>
  
  <pre><code>CLASS                  | TABLE              | TYPE
  -----------------------+--------------------+------
  BaseEntity (abstract)  | BaseTable          |
  Inherited_TPH          | BaseTable          |  1
  Inherited_TPT          | Inherited_TPT      |  2
  </code></pre>
  
  <p>The column used as discrimator in the table is called <code>Type</code></p>
  
  <p>Based on <a href="https://stackoverflow.com/a/6034803/314334">this answer</a> I added an abstract class <code>Intermediate_TPH</code> as intermediate layer:</p>
  
  <p><img src="https://i.stack.imgur.com/xvDlm.png" alt="Diagram of classes and mapping"></p>
  
  <p>Some sample data: Entry with <code>ID=3</code> is of type <code>Inherited_TPT</code></p>
  
  <p><img src="https://i.stack.imgur.com/UkG7D.png" alt="Data"></p>
  
  <h2>Code</h2>
  
  <p>These are my entity classes and my context class:</p>
  
  <pre><code>class MyContext : DbContext
  {
      public MyContext ( string connectionString )
          : base ( connectionString )
      {
      }
  
      public DbSet&lt;Inherited_TPH&gt; TPH_Set { get; set; }
      public DbSet&lt;Inherited_TPT&gt; TPT_Set { get; set; }
      public DbSet&lt;SomethingElse&gt; Another_Set { get; set; }
  
      protected override void OnModelCreating ( DbModelBuilder modelBuilder )
      {
          modelBuilder
          .Entity&lt;BaseEntity&gt; ()
          .ToTable ( "BaseTable" );
  
          modelBuilder
          .Entity&lt;Inherited_TPH&gt; ()
          .Map ( t =&gt; t.Requires ( "Type" ).HasValue ( 1 ) );
  
          modelBuilder
          .Entity&lt;Intermediate_TPT&gt; ()
          .Map ( t =&gt; t.Requires ( "Type" ).HasValue ( 2 ) );
  
          modelBuilder
          .Entity&lt;Intermediate_TPT&gt; ()
          .Map&lt;Inherited_TPT&gt; ( t =&gt; t.ToTable ( "Inherited_TPT" ) ); 
  
          modelBuilder
          .Entity&lt;SomethingElse&gt; ()
          .ToTable ( "SomethingElse" )
          .HasKey ( t =&gt; t.Id );
      }
  }
  
  public abstract class BaseEntity
  {
      public virtual int Id { get; set; }
      public virtual string Title { get; set; }
  }
  public class Inherited_TPH : BaseEntity
  {
  }
  public abstract class Intermediate_TPT : BaseEntity
  {
  }
  public class Inherited_TPT : Intermediate_TPT
  {
      public virtual string Comment { get; set; }
  }
  public class SomethingElse
  {
      public virtual string Description { get; set; }
      public virtual int Id { get; set; }
  }
  </code></pre>
  
  <p>Running the following code will give me an error.</p>
  
  <pre><code>    static void Main ( string[] args )
      {
          Database.SetInitializer&lt;MyContext&gt; ( null );
          var ctx = new MyContext ( @"Data Source=(local);Initial Catalog=nh_ef;Integrated Security=true" );
          try
          {
              // Accessing Inherited_TPH works just fine
              foreach ( var item in ctx.TPH_Set ) Console.WriteLine ( "{0}: {1}", item.Id, item.Title );
              // Accessing Inherited_TPT works just fine
              foreach ( var item in ctx.TPT_Set ) Console.WriteLine ( "{0}: {1} ({2})", item.Id, item.Title, item.Comment );
              // The rror occurs when accessing ANOTHER entity:
              foreach ( var item in ctx.Another_Set ) Console.WriteLine ( "{0}: {1}", item.Id, item.Description );
          }
          catch ( Exception ex )
          {
              Console.WriteLine ( ex.Message );
              if( ex.InnerException != null ) { Console.WriteLine ( ex.InnerException.Message ); }
          }
      }
  </code></pre>
  
  <h2>Output</h2>
  
  <p>The program produces the following output:</p>
  
  <blockquote>
    <p>1: Simpson<br>
    2: Johnson<br>
    3: Smith (More details about SMITH)<br>
    4: Miller (More details about MILLER)<br>
    An error occurred while preparing the command definition. See the inner exception for details.  </p>
    
    <p>(26,10) : error 3032: Problem in mapping fragments starting at lines 14, 26:EntityTypes PoC.Inherited_TPH, PoC.Inherited_TPT are being mapped to the same rows in table BaseEntity. Mapping conditions can be used to distinguish the rows that these types are mapped to.</p>
  </blockquote>
  
  <h2>Question</h2>
  
  <p>As you can see, the mapping <strong>seems to work</strong> because I can load all data from <code>Inherited_TPT</code> and <code>Inherited_TPH</code>. But when accessing another entity, <strong>I get an exception</strong>.</p>
  
  <p>How do I need to configure the mapping to get rid of this error and be able to access the existing database structure?</p>