title: Multilevel Inheritance in Entity Framework by TPT strategy
body: <p>Suppose this is the case, I want a separate table for every class (including abstract).</p>
  
  <p><img src="https://i.stack.imgur.com/LZMEN.png" alt="DB Schema Snapshot"></p>
  
  <p>Here is the DBContext:</p>
  
  <pre><code>public class AIOmedicareContext : DbContext
  {
  
      public AIOmedicareContext() : base("name=AIOmedicareConnectionString")
      {
  
      }
  
      public DbSet&lt;User&gt; Users { get; set; }
  
      public DbSet&lt;Role&gt; Roles { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
  
          modelBuilder.Entity&lt;Role&gt;()
              .ToTable("Roles")
              .HasKey(p =&gt; p.PrimaryKey)
              .Property(p =&gt; p.PrimaryKey)
              .HasColumnName("RoleID")
              .HasColumnOrder(1);
  
          // Moved all Person related configurations to PersonConfiguration class.
          modelBuilder.Configurations.Add(new PersonConfiguration());
  
          // Moved all User related configurations to UserConfiguration class.
          modelBuilder.Configurations.Add(new UserConfiguration());
  
          // Moved all Employee related configurations to EmployeeConfiguration class.
          modelBuilder.Configurations.Add(new EmployeeConfiguration());
  
  
  
          base.OnModelCreating(modelBuilder);
      }
  
  }
  </code></pre>
  
  <p>Entity Confugrations</p>
  
  <p><strong>PersonConfiguration.cs</strong></p>
  
  <pre><code>public class PersonConfiguration : EntityTypeConfiguration&lt;Person&gt;
  {
      public PersonConfiguration()
      {
          // Database table name
          this.ToTable("Person")
              .HasKey&lt;Guid&gt;(p =&gt; p.PrimaryKey);
  
          // every person have atmost one account.
          this.HasOptional&lt;Account&gt;(s =&gt; s.Account);
  
          // PrimaryKey property mapped to PersonID column in database.
          this.Property(p =&gt; p.PrimaryKey)
              .HasColumnName("PersonID")
              .HasColumnOrder(1);
  
          // a person can have many contacts.
          this.HasMany&lt;Contact&gt;(c =&gt; c.Contacts)
              .WithRequired(p =&gt; p.Person)
              .HasForeignKey(c =&gt; c.PersonID);
  
          // a person can have many addresses.
          this.HasMany&lt;Address&gt;(p =&gt; p.Addresses)
              .WithRequired(a =&gt; a.Person)
              .HasForeignKey(a =&gt; a.PersonID);
      }
  }
  </code></pre>
  
  <p><strong>EmployeeConfiguration.cs</strong></p>
  
  <pre><code>class EmployeeConfiguration : EntityTypeConfiguration&lt;Employee&gt;
  {
      public EmployeeConfiguration()
      {
          this.ToTable("Employees")
              .HasKey(p =&gt; p.PrimaryKey)
              .Property(e =&gt; e.PrimaryKey)
              .HasColumnName("EmployeeID")
              .HasColumnOrder(1);
  
          this.HasMany&lt;Role&gt;(u =&gt; u.Roles)
              .WithMany(e =&gt; e.Employees)
              .Map(re =&gt; {
                  re.MapLeftKey("EmployeeRefID");
                  re.MapRightKey("RoleRefID");
                  re.ToTable("EmployeeRole");
              });
      }
  }
  </code></pre>
  
  <p><strong>UserConfiguration.cs</strong></p>
  
  <pre><code>class UserConfiguration : EntityTypeConfiguration&lt;User&gt;
  {
      public UserConfiguration()
      {
          this.ToTable("Users")
              .HasKey(p =&gt; p.PrimaryKey)
              .Property(u =&gt; u.PrimaryKey)
              .HasColumnName("UserID")
              .HasColumnOrder(1);
  
          this.HasMany(u =&gt; u.UserLogs)
              .WithRequired(ul =&gt; ul.User)
              .HasForeignKey(u =&gt; u.UserID);
  
          this.HasMany(u =&gt; u.Permissions)
              .WithOptional(p =&gt; p.User)
              .HasForeignKey(p =&gt; p.UserID);
      }
  }
  </code></pre>
  
  <p>I am trying to create a object of <strong>User</strong> through a console application but it throwing the following exceptions.</p>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          User user = new User();
          user.FirstName = "Sandeep";
          user.LastName = "Sihari";
          user.Age = 15;
          user.Sex = "Male";
          user.JoiningDate = DateTime.Now;
          user.UserName = "qwerty";
          user.HashedPassword = CalculateHash("qwerty", user.UserName);
  
          Contact contact = new Contact();
          contact.Type = new ContactType() { Name = "Office" };
          contact.Name = "Sandeep";
          contact.Number = "123456";
          contact.Description = "No description";
          user.AddContact(contact);
  
          ISessionFactory factory = new SessionFactory();
          IUnitOfWork unitOfWork = factory.CurrentUoW;
          IRepository repository = new Repository(unitOfWork);
          unitOfWork.BeginTransaction();
  
          repository.AddEntity(user);
  
          unitOfWork.CommitTransaction();
  
          Console.WriteLine("Operation completed.");
  
          Console.ReadLine();
      }
  
      private static string CalculateHash(string clearTextPassword, string salt)
      {
          // Convert the salted password to a byte array
          byte[] saltedHashBytes = Encoding.UTF8.GetBytes(clearTextPassword + salt);
          // Use the hash algorithm to calculate the hash
          HashAlgorithm algorithm = new SHA256Managed();
          byte[] hash = algorithm.ComputeHash(saltedHashBytes);
          // Return the hash as a base64 encoded string to be compared to the stored password
          return Convert.ToBase64String(hash);
      }
  }
  </code></pre>
  
  <p>Here is the error.</p>
  
  <blockquote>
    <p>Error retrieving values from ObjectStateEntry. See inner exception for details.</p>
  </blockquote>
  
  <p>And inner exception is:</p>
  
  <blockquote>
    <p>(23,10) : error 3032: Problem in mapping fragments starting at lines 23, 34:EntityTypes AIOmedicare.DAL.EF.Employee, AIOmedicare.DAL.EF.Customer, AIOmedicare.DAL.EF.User are being mapped to the same rows in table Person. Mapping conditions can be used to distinguish the rows that these types are mapped to.</p>
  </blockquote>
  
  <p>Migrations are applied successfully, and have verified the tables are present through SQL Server Management Studio.</p>
  
  <p>What am I missing ?</p>