title: How to map child entities to same table in EF Core
body: <p>I am using EF Core and I need to map child entities to the same table, because they are of the same type.</p>
  
  <p>Imagine the following scenario of class hierarchy:</p>
  
  <pre><code>public class Grandpa
  {
      public Guid Id { get; set; }
      public Father Robert { get; set; }
      public Father Jack { get; set; }
  }
  
  public class Father
  {
      public Guid Id { get; set; }
      // I use this to reference Grandpa
      public Guid GrandpaId { get; set; }
      public List&lt;Son&gt; MySons { get; set; }
  }
  
  public class Son
  {
      public Guid Id { get; set; }
      // I use this to reference Father
      public Guid FatherId { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Now the issue is the following: in my scenario (the above is the abstraction replicating my real scenario), I cannot make <code>Robert</code> and <code>Jack</code> an array - they need to be there separately. The other thing is, the <code>Grandpa</code> entity owns 2 <code>Father</code> entities, and each <code>Father</code> entity owns a list of <code>Son</code> entities.</p>
  
  <p>I want to tell ModelBuilder to generate my tables in such a way that there is ONE TABLE for all <code>Father</code> entities, and ONE TABLE for all <code>Son</code> entities - because they are all the same entities, and they should never be placed in different tables as their primary keys and foreign keys (IDs) clearly reference who they belong to.</p>
  
  <p>Now, you know, in plain SQL, it's easy to achieve such a structure, very simple to write the SQL code for this. However, how do I do it with EF Core?</p>
  
  <p>I've tried:</p>
  
  <p>1) Specifying all primary keys and foreign keys in model builder, per entity
  2) Setup the owned property hierarchy in ModelBuilder using </p>
  
  <pre><code>modelBuilder.Entity&lt;Father&gt;()
    .ToTable("Fathers");
  modelBuilder.Entity&lt;Son&gt;()
    .ToTable("Sons");
  modelBuilder.Entity&lt;Grandpa&gt;(g =&gt; 
  {
    x.OwnsOne(y =&gt; y.Robert, z =&gt; 
    { 
      z.OwnsMany(a =&gt; a.MySons, b =&gt; 
      {
        b.HasKey(k=&gt;k.Id); 
        b.ToTable("Sons"); 
      }); 
      z.ToTable("Fathers"); 
    });
    x.OwnsOne(y =&gt; y.Jack, z =&gt; 
    { 
      z.OwnsMany(a =&gt; a.MySons, b =&gt; 
      {
        b.HasKey(k=&gt;k.Id); 
        b.ToTable("Sons"); 
      }); 
      z.ToTable("Fathers"); 
    });
  });
  </code></pre>
  
  <p>However, there are errors saying:</p>
  
  <pre><code>Cannot use table 'Fathers' for entity type 'Grandpa.Father#Robert' since it is being used for entity type 'Grandpa.Father#Jack' and there is no relationship between their primary keys.
  </code></pre>
  
  <p>I must mention that these entities are part of the same <code>DbContext</code> - and they need to remain so, preferably.</p>
  
  <p>The way EF Core maps or fails to map entities is wrong - if I remove the table name specifications, it just creates separate tables for my entities of the same type, which is wrong. I am not even in the case of having the same entity declared in a different namespace/duplicated, but it's actually the same namespace, the same entity, the <code>Grandpa</code> entity has two <code>Father</code> entities, and each <code>Father</code> entity has a <code>List&lt;Son&gt;</code>.</p>