title: Entity Framework one-to-many with table-per-hierarchy creates one foreign key column per subclass
body: <p>I have a <code>Garage</code> which contains <code>Cars</code> and <code>Motorcycles</code>.  Cars and motorcycles are <code>Vehicles</code>.  Here they are:</p>
  
  <pre><code>public class Garage
  {
      public int Id { get; set; }
      public virtual List&lt;Car&gt; Cars { get; set; }
      public virtual List&lt;Motorcycle&gt; Motorcycles { get; set; }
  
      public Garage()
      {
          Cars = new List&lt;Car&gt;();
          Motorcycles = new List&lt;Motorcycle&gt;();
      }
  }
  
  public abstract class Vehicle
  {
      public int Id { get; set; }
      public string Make { get; set; }
      public string Model { get; set; }
  }
  
  public class Car : Vehicle
  {
      public int GarageId { get; set; }
      public virtual Garage Garage { get; set; }
      // some more properties here...
  }
  
  public class Motorcycle : Vehicle
  {
      public int GarageId { get; set; }
      public virtual Garage Garage { get; set; }
      // some more properties here...
  }
  </code></pre>
  
  <p>Why do Car and Motorcycle each have a GarageId and Garage property?  If I push those properties up to the Vehicle superclass, EF complains and tells me navigation properties must reside in concrete classes.</p>
  
  <p>Moving on, here's my DbContext:</p>
  
  <pre><code>public class DataContext : DbContext
  {
      public DbSet&lt;Garage&gt; Garages { get; set; }
      public DbSet&lt;Vehicle&gt; Vehicles { get; set; }
      public DbSet&lt;Car&gt; Cars { get; set; }
      public DbSet&lt;Motorcycle&gt; Motorcycles { get; set; }
  
      public DataContext()
          : base("GarageExample")
      {
  
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove&lt;OneToManyCascadeDeleteConvention&gt;();
          modelBuilder.Conventions.Remove&lt;ManyToManyCascadeDeleteConvention&gt;();
      }
  }
  </code></pre>
  
  <p>And here's a short program to play with my toys:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Database.SetInitializer&lt;DataContext&gt;(new DropCreateDatabaseAlways&lt;DataContext&gt;());
  
          using (var db = new DataContext())
          {
              var car1 = new Car { Make = "Subaru", Model = "Legacy" };
              var car2 = new Car { Make = "Porche", Model = "911" };
  
              var bike1 = new Motorcycle { Make = "Suzuki", Model = "GS500" };
              var bike2 = new Motorcycle { Make = "Kawasaki", Model = "Ninja" };
  
              var garage = new Garage();
  
              garage.Cars.Add(car1);
              garage.Cars.Add(car2);
              garage.Motorcycles.Add(bike1);
              garage.Motorcycles.Add(bike2);
  
              db.Garages.Add(garage);
  
              db.SaveChanges();
          }
      }
  }
  </code></pre>
  
  <p>The program runs, and produces the following <strong>Vehicles</strong> table:</p>
  
  <pre><code>Id Make     Model  GarageId GarageId1 Discriminator
  1  Subaru   Legacy 1        null      Car
  2  Porche   911    1        null      Car
  3  Suzuki   GS500  null     1         Motorcycle
  4  Kawasaki Ninja  null     1         Motorcycle
  </code></pre>
  
  <p>With both Car and Motorcycle having their own GarageId and Garage properties, it seems that each subclass is creating its own foreign key to garage.  How do I tell EF (via the fluent api, if possible) that Car.Garage and the Motorcycle.Garage are the same thing, and should use the same column?</p>
  
  <p>This is the <strong>Vehicles</strong> table I want, of course:</p>
  
  <pre><code>Id Make     Model  GarageId Discriminator
  1  Subaru   Legacy 1        Car
  2  Porche   911    1        Car
  3  Suzuki   GS500  1        Motorcycle
  4  Kawasaki Ninja  1        Motorcycle
  </code></pre>