title: Derived types generating their own ForeignKey field in code first using TPH
body: <p>I'm using EF Core and implementing inheritance in a table-per-hierarchy (TPH) approach. I've found that once I added the hierarchy objects to my data model the migration is adding a foreign key for the base table and what appears to be a duplicate that the descendant classes are using.</p>
  
  <p>Relationship is from <code>Transaction</code> (parent) to <code>TransactionItem</code> (child).</p>
  
  <p>Object hierarchy:</p>
  
  <ul>
  <li>TransactionItem (Base class)
  
  <ul>
  <li>Attachment</li>
  <li>Check</li>
  <li>Envelope</li>
  <li>Stub</li>
  </ul></li>
  </ul>
  
  <p>Using Fluent API to define the relationship between <code>Transaction</code> and <code>TransactionItem</code> using the <code>TransactionItem.TransactionId</code> field as the foreign key:</p>
  
  <pre><code>modelBuilder.Entity&lt;TransactionItem&gt;(t =&gt;
  {
      t.ToTable("TransactionItem");
      t.HasKey(a =&gt; a.Id);
      t.Property(a =&gt; a.ConcurrencyStamp)
          .IsRequired()
          .IsConcurrencyToken();
      t.Property(a =&gt; a.Created)
          .IsRequired();
      t.HasOne(a =&gt; a.Transaction)
          .WithMany(a =&gt; a.TransactionItems)
          .HasForeignKey(a =&gt; a.TransactionId)
          .OnDelete(DeleteBehavior.Restrict);
      t.HasDiscriminator&lt;string&gt;("ItemType")
          .HasValue&lt;Check&gt;("Check")
          .HasValue&lt;Stub&gt;("Stub")
          .HasValue&lt;Envelope&gt;("Envelope")
          .HasValue&lt;Attachment&gt;("Attachment");
  });
  </code></pre>
  
  <p>The migration Up method is creating the base class table (TPH) with  <code>TransactionId</code> and <code>TransactionId2</code> columns, each relating to the same parent table:</p>
  
  <pre><code>migrationBuilder.CreateTable(
  name: "TransactionItem",
  columns: table =&gt; new
  {
      Id = table.Column&lt;int&gt;(nullable: false)
          .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
      ConcurrencyStamp = table.Column&lt;string&gt;(nullable: false),
      Created = table.Column&lt;DateTimeOffset&gt;(nullable: false),
      ItemType = table.Column&lt;string&gt;(nullable: false),
      PageName = table.Column&lt;string&gt;(nullable: true),
      Sequence = table.Column&lt;int&gt;(nullable: false),
      TransactionId = table.Column&lt;int&gt;(nullable: false),
      TransactionId2 = table.Column&lt;int&gt;(nullable: true),
      CheckType = table.Column&lt;string&gt;(nullable: true),
      Micr = table.Column&lt;string&gt;(nullable: true),
      MicrValid = table.Column&lt;bool&gt;(nullable: true),
      OcrId = table.Column&lt;string&gt;(nullable: true)
  },
  constraints: table =&gt;
  {
      table.PrimaryKey("PK_TransactionItem", x =&gt; x.Id);
      table.ForeignKey(
          name: "FK_TransactionItem_Transaction_TransactionId",
          column: x =&gt; x.TransactionId,
          principalTable: "Transaction",
          principalColumn: "Id",
          onDelete: ReferentialAction.Restrict);
      table.ForeignKey(
          name: "FK_TransactionItem_Transaction_TransactionId2",
          column: x =&gt; x.TransactionId2,
          principalTable: "Transaction",
          principalColumn: "Id",
          onDelete: ReferentialAction.Restrict);
  });
  </code></pre>
  
  <p>When I look at the ModelSnapshot.cs code I see the following relationships specific to the objects in the inheritance model establishing a foreign key for the derived class entities using the <code>TransactionId2</code> column.</p>
  
  <pre><code>modelBuilder.Entity("SampleProject.Data.DataModels.TransactionItem", b =&gt;
      {
          b.HasOne("SampleProject.Data.DataModels.Transaction", "Transaction")
              .WithMany("TransactionItems")
              .HasForeignKey("TransactionId");
      });
  
  modelBuilder.Entity("SampleProject.Data.DataModels.Attachment", b =&gt;
      {
          b.HasOne("SampleProject.Data.DataModels.Transaction")
              .WithMany("Attachments")
              .HasForeignKey("TransactionId2");
      });
  
  modelBuilder.Entity("SampleProject.Data.DataModels.Check", b =&gt;
      {
          b.HasOne("SampleProject.Data.DataModels.Transaction")
              .WithMany("Checks")
              .HasForeignKey("TransactionId2");
      });
  
  modelBuilder.Entity("SampleProject.Data.DataModels.Envelope", b =&gt;
      {
          b.HasOne("SampleProject.Data.DataModels.Transaction")
              .WithMany("Envelopes")
              .HasForeignKey("TransactionId2");
      });
  
  modelBuilder.Entity("SampleProject.Data.DataModels.Stub", b =&gt;
      {
          b.HasOne("SampleProject.Data.DataModels.Transaction")
              .WithMany("Stubs")
              .HasForeignKey("TransactionId2");
      });
  </code></pre>
  
  <p>I'm obviously missing something but can't figure out what it is. I want to be able to use inheritance hierarchy with TPH approach and a single foreign key field from the base class table (<code>TransactionItem</code>) to the parent (<code>Transaction</code>). I appreciate any help that you can provide.</p>
  
  <p>Thanks!</p>