#TPH # TPT #TPC #ENTITY #ENTITYFRAMEWORK
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 that inherits from a base Entity are mapped to that table.

C# Implementation Code for TPH
public class Course { [Key] public int CourseId { get; set; } public string Name { get; set; } public string Details { get; set; } public string Trainer { get; set; } } public class OnlineCourse : Course { public string URL { get; set; } } public class OfflineCourse : Course { public string Address { get; set; } }Entity Framework Code First Mapping for TPH
modelBuilder.Entity<Course>() .Map<OnlineCourse >(m => m.Requires("Type").HasValue("OnlineCourse ")) .Map<OfflineCourse >(m => m.Requires("Type").HasValue("OfflineCourse "));Table-per-Concrete-Type (TPC)
The TPC inheritance states that each concrete class (a class which can be instantiated) 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.

C# Implementation Code for TPC
public abstract class Course //abstract class { [Key] public int CourseId { get; set; } public string Name { get; set; } public string Details { get; set; } public string Trainer { get; set; } } public class OnlineCourse : Course //concrete class { public string URL { get; set; } } public class OfflineCourse : Course //concrete class { public string Address { get; set; } }Entity Framework Code First Mapping for TPC
modelBuilder.Entity<OnlineCourse >().Map(m => { m.MapInheritedProperties(); m.ToTable("OnlineCourse "); }); modelBuilder.Entity<OfflineCourse >().Map(m => { m.MapInheritedProperties(); m.ToTable("OfflineCourse "); });Note
The TPC inheritance is commonly used to inherit basic features.
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.

C# Implementation Code for TPT
public class Course { [Key] public int CourseId { get; set; } public string Name { get; set; } public string Details { get; set; } public string Trainer { get; set; } } public class OnlineCourse : Course { public string URL { get; set; } } public class OfflineCourse : Course { public string Address { get; set; } }Entity Framework Code First Mapping for TPT
modelBuilder.Entity<Course>().ToTable("Course"); modelBuilder.Entity<OnlineCourse >().ToTable("OnlineCourse "); modelBuilder.Entity<OfflineCourse >().ToTable("OfflineCourse ");
No comments:
Post a Comment