title: EntityFramework - Table per Type (TPT) Inheritance and Mapping Relationships with CodeFirst
body: <p>I have the following Entities which I am persisting using EntityFramework CodeFirst:</p>
  
  <pre><code>public class User {
  
       RedGroup RedGroup { get; protected set; }
       virtual ICollection&lt;GreenGroup&gt; GreenGroups { get;  }
  
       int Id { get; protected set; }
       int? RedGroupId { get; protected set; }
  }
  
  public abstract class Group { 
  
       int Id { get; protected set; }
       virtual ICollection&lt;User&gt; Users { get; protected set; }
  }
  
  public class RedGroup : Group {
      // Other properties
  }
  
  public class GreenGroup : Group {
      // Other properties
  }
  </code></pre>
  
  <p>Essentially, the user can belong to zero or one red groups, and more than one green group.  Each group has a collection of users that belong to it.</p>
  
  <p>I am trying to set up EF using CodeFirst with TPT and am having trouble sorting the mappings.  At the moment, I have the following in <code>OnModelCreating</code>:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
       modelBuilder.Configurations.Add(new RedGroupMap());
       modelBuilder.Configurations.Add(new GreenGroupMap());
       modelBuilder.Configurations.Add(new UserMap());
  }
  </code></pre>
  
  <p>These are the mapping classes:</p>
  
  <pre><code> public abstract class GroupMap&lt;T&gt; : EntityTypeConfiguration&lt;T&gt;
       where T : Group {
  
      public GroupMap() {
  
          this.ToTable("Groups");
  
          this.HasKey(t =&gt; t.Id);
          this.Property(t =&gt; t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("Id");
  
          // Also has other  non-relationship mappings
      }
  
   }
  
   public class RedGroupMap() : GroupMap&lt;RedGroup&gt; {
  
      public RedGroupMap() {
  
          this.ToTable("RedGroups");
  
          // Also has other  non-relationship mappings
      }
   }
  
   public class GreenGroupMap() : GroupMap&lt;GreenGroup&gt; {
  
      public GreenGroupMap() {
  
          this.ToTable("GreenGroups");
  
          this.HasMany(c =&gt; c.Users)
              .WithMany(p =&gt; p.GreenGroups)
              .Map(m =&gt;
              {
                  m.MapLeftKey("GreenGroupId");
                  m.MapRightKey("UserId");
                  m.ToTable("Users_GreenGroups");
              });
  
         // Also has other non-relationship mappings
      }
  }
  
  public class UserMap() : EntityTypeConfiguration&lt;User&gt; {
  
    this.ToTable("Users");
    this.HasKey(t =&gt; t.Id);
    this.Property(t =&gt; t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("Id");
  
    this.HasOptional(t =&gt; t.RedGroup)
              .WithMany(t =&gt; t.Users)
              .Map(x =&gt; x.MapKey("RedGroupId"))
              .WillCascadeOnDelete(false);
  }
  </code></pre>
  
  <p>I am getting the following runtime error:</p>
  
  <p><code>Users: FromRole: NavigationProperty 'Users' is not valid. Type 'RedGroup' of FromRole 'User_RedGroup_Target' in AssociationType 'User_RedGroup' must exactly match with the type 'GreenGroup' on which this NavigationProperty is declared on.</code></p>
  
  <p>Afraid I'm stumped on how to set up this. </p>
  
  <p>How can I set up the EntityFramework mappings to allow a Table per Type hierarchy?</p>