HI WELCOME TO SIRIS

Understanding Inheritance in Entity Framework

Inheritance in the Entity Framework is similar to inheritance for classes in C#. In Entity Framework, you can map an inheritance hierarchy to single or multiple database tables based on your requirements. In this article, you will learn how to map your inheritance hierarchy to database tables in SQL Server.

Types of inheritance in Entity Framework

Entity Framework supports three types of inheritances as given below-
  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 that inherits from a base Entity are mapped to that table.

    C# Implementation Code for TPH

    1. public class Course
    2. {
    3. [Key]
    4. public int CourseId { get; set; }
    5. public string Name { get; set; }
    6. public string Details { get; set; }
    7. public string Trainer { get; set; }
    8. }
    9.  
    10. public class OnlineCourse : Course
    11. {
    12. public string URL { get; set; }
    13. }
    14.  
    15. public class OfflineCourse : Course
    16. {
    17. public string Address { get; set; }
    18. }

    Entity Framework Code First Mapping for TPH

    1. modelBuilder.Entity<Course>()
    2. .Map<OnlineCourse >(m => m.Requires("Type").HasValue("OnlineCourse "))
    3. .Map<OfflineCourse >(m => m.Requires("Type").HasValue("OfflineCourse "));

    Note

    By default, Entity Framework supports TPH inheritance, if you don't define any mapping details for your inheritance hierarchy.
  2. 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

    1. public abstract class Course //abstract class
    2. {
    3. [Key]
    4. public int CourseId { get; set; }
    5. public string Name { get; set; }
    6. public string Details { get; set; }
    7. public string Trainer { get; set; }
    8. }
    9.  
    10. public class OnlineCourse : Course //concrete class
    11. {
    12. public string URL { get; set; }
    13. }
    14.  
    15. public class OfflineCourse : Course //concrete class
    16. {
    17. public string Address { get; set; }
    18. }

    Entity Framework Code First Mapping for TPC

    1. modelBuilder.Entity<OnlineCourse >().Map(m =>
    2. {
    3. m.MapInheritedProperties();
    4. m.ToTable("OnlineCourse ");
    5. });
    6.  
    7. modelBuilder.Entity<OfflineCourse >().Map(m =>
    8. {
    9. m.MapInheritedProperties();
    10. m.ToTable("OfflineCourse ");
    11. });

    Note

    The TPC inheritance is commonly used to inherit basic features.
  3. 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

    1. public class Course
    2. {
    3. [Key]
    4. public int CourseId { get; set; }
    5. public string Name { get; set; }
    6. public string Details { get; set; }
    7. public string Trainer { get; set; }
    8. }
    9.  
    10. public class OnlineCourse : Course
    11. {
    12. public string URL { get; set; }
    13. }
    14.  
    15. public class OfflineCourse : Course
    16. {
    17. public string Address { get; set; }
    18. }

    Entity Framework Code First Mapping for TPT

    1. modelBuilder.Entity<Course>().ToTable("Course");
    2. modelBuilder.Entity<OnlineCourse >().ToTable("OnlineCourse ");
    3. modelBuilder.Entity<OfflineCourse >().ToTable("OfflineCourse ");

    Note

    TPH inheritance patterns generally deliver better performance in the Entity Framework than TPT inheritance patterns, because TPT patterns can result in complex join queries.
What do you think?
I hope you will enjoy the tips while programming with Entity Framework. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.