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
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
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
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 TPHpublic 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; }
}
modelBuilder.Entity<Employee>()
.Map<EmployeeDetails>(m => m.Requires("Type").HasValue("EmployeeDetails"))
.Map<EmployeeLogin>(m => m.Requires("Type").HasValue("EmployeeLogin"));
2) Table-per-Type (TPT).Map<EmployeeDetails>(m => m.Requires("Type").HasValue("EmployeeDetails"))
.Map<EmployeeLogin>(m => m.Requires("Type").HasValue("EmployeeLogin"));
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 TPTpublic 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; }
}
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.modelBuilder.Entity<EmployeeLogin>().ToTable("EmployeeLogin");
modelBuilder.Entity<EmployeeDetails>().ToTable("EmployeeDetails");
Table-per-Concrete-Type (TPC)
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 TPCpublic 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; }
}
modelBuilder.Entity<EmployeeDetails>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("EmployeeDetails");
});
modelBuilder.Entity<EmployeeLogin>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("EmployeeLogin");
});
{
m.MapInheritedProperties();
m.ToTable("EmployeeDetails");
});
modelBuilder.Entity<EmployeeLogin>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("EmployeeLogin");
});