title: All objects in the EntitySet must have unique primary > keys
body: <p>I am facing an issue with entity framework code first and inheritance. Here’s my model : </p>
  
  <pre><code>public class Person
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  
  public class Student : Person
  {
      // Student properties
  }
  
  public class Teacher : Person
  {
      // Teacher properties
  }
  </code></pre>
  
  <p>Those 3 classes are mapped to 3 tables with entity framework. Each of them has a column « Id » (Table per type).
  So far, this is working well.</p>
  
  <p>The problem is that <strong>a Student can also be a Teacher</strong>. 
  When I try to get both the Student and the Teacher objects in the same DbContext, I get the error : </p>
  
  <blockquote>
    <p>All objects in the EntitySet 'Context.People' must have unique primary
    keys. However, an instance of type 'Student' and an instance of type
    Teacher' both have the same primary key value.</p>
  </blockquote>
  
  <p>Is there a way to work around this problem ?</p>
  
  <p>EDIT : Here's more details about my code :</p>
  
  <pre><code>using(var context = new CoreContext())
  {
      var testStudents = context.Students.ToList();
      var testTeachers = context.Teachers.ToList();
  }
  </code></pre>
  
  <p><em>(Error happens when context.Teachers.ToList() is called).</em></p>
  
  <pre><code>public class CoreContext : DbContext
  {
      public DbSet&lt;Student&gt; Students { get; set; }
      public DbSet&lt;Teacher&gt; Teachers { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
           modelBuilder.Configurations.Add(new PersonMap());
           modelBuilder.Configurations.Add(new StudentMap());
           modelBuilder.Configurations.Add(new TeacherMap());
      }
  }
  
  internal class PersonMap: EntityTypeConfiguration&lt;Person&gt;
  {
      public PersonMap() 
      {
          this.HasKey(i =&gt; i.Id);
          this.Property(i =&gt; i.Id).HasColumnName("Id");
          this.Property(i =&gt; i.Name).HasColumnName("Name");
          this.ToTable("People");
      }
  }
  
  internal class StudentMap : EntityTypeConfiguration&lt;Student&gt;
  {
      public StudentMap() 
      {
          this.ToTable("Students");
      }
  }
  
  internal class TeacherMap : EntityTypeConfiguration&lt;Teacher&gt;
  {
      public TeacherMap() 
      {
          this.ToTable("Teachers");
      }
  }
  </code></pre>