h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Type of Inheritance in Entity Framework

Type of Inheritance in Entity Framework

1) Table-Per-Hierarchy(TPH):

The TPH inheritance states that all entities, in a hierarchy of entities, are mapped to a single table in storage schema. It means, there is only one table in database and different Entity types in Entity model
public class Employee
{
 public int EmployeeID { get; set; }
 public string EmployeeName { get; set; }
}
public class EmployeeDetails : Employee
{
 public string EmployeeAddress { get; set; }
 public string EmployeeMobile { get; set; }
}
public class EmployeeLogin : Employee
{
 public string UserName { get; set; }
 public string Password { get; set; }
}
Entity Framework Code First Mapping for TPH
modelBuilder.Entity<Employee>()
 .Map<EmployeeDetails>(m => m.Requires("Type").HasValue("EmployeeDetails"))
 .Map<EmployeeLogin>(m => m.Requires("Type").HasValue("EmployeeLogin"));

2) Table-per-Type (TPT)
        The TPT inheritance states that each entity in the hierarchy of entities is mapped to a separate table in storage schema. It means, there is separate table in database to maintain data for each Entity Type.
public class Employee
{
 public int EmployeeID { get; set; }
 public string EmployeeName { get; set; }
}
public class EmployeeDetails : Employee
{
 public string EmployeeAddress { get; set; }
 public string EmployeeMobile { get; set; }
}
public class EmployeeLogin : Employee
{
 public string UserName { get; set; }
 public string Password { get; set; }
}
Entity Framework Code First Mapping for TPT
modelBuilder.Entity<Employee>().ToTable("Employee");
modelBuilder.Entity<EmployeeLogin>().ToTable("EmployeeLogin");
modelBuilder.Entity<EmployeeDetails>().ToTable("EmployeeDetails");
Table-per-Concrete-Type (TPC)
          The TPC inheritance states that each derived entity (not base entity) in the hierarchy of entities is mapped to a separate table in storage schema.
          It means, there is separate table in database to maintain data for each derived entity type.
          This inheritance occurs when we have two tables with overlapping fields in the database like as a table and its history table that has all historical data which is transferred from the main table to it.
public abstract class Employee
{
 public int EmployeeID { get; set; }
 public string EmployeeName { get; set; }
}
public class EmployeeDetails : Employee
{
 public string EmployeeAddress { get; set; }
 public string EmployeeMobile { get; set; }
}
public class EmployeeLogin : Employee
{
 public string UserName { get; set; }
 public string Password { get; set; }
}
Entity Framework Code First Mapping for TPC
modelBuilder.Entity<EmployeeDetails>().Map(m =>
{
 m.MapInheritedProperties();
 m.ToTable("EmployeeDetails");
});
modelBuilder.Entity<EmployeeLogin>().Map(m =>
{
 m.MapInheritedProperties();
 m.ToTable("EmployeeLogin");
});