Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding Inheritance in Entity Framework

  Author : Shailendra Chauhan
Posted On : 03 Dec 2013
Total Views : 46,030   
Updated On : 26 Sep 2016
 

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.

    Table-per-Hierarchy (TPH)

    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 "));
    

    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.

    Table-per-Concrete-Type (TPC)

    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.

  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.

    Table-per-Type (TPT)

    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 ");
    

    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.



Free Interview Books
 
COMMENTS (0)
3 AUG
ASP.NET Core with Angular4 Development (Online)

07:00 AM - 9:00 AM IST(+5:30 GMT)

Know More
29 JUL
Angular2 and Angular4 Development (Online)

08:00 AM - 10:00 AM IST(+5.30 GMT)

Know More
29 JUL
ASP.NET MVC with Angular4 Development (Classroom)

09:30 AM - 11:00 AM IST (+5:30GMT)

Know More
29 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

09:00 AM - 11:00 AM IST(+5:30 GMT)

Know More
26 JUL
ASP.NET MVC with Angular4 Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

Know More
22 JUL
MEAN Stack 2 Development (Classroom)

04:00 PM - 05:30 PM IST(+5:30GMT)

20 JUL
Hadoop Development cum Administration (Online)

07:00 AM - 09:00 AM IST(+5:30 GMT)

17 JUL
NodeJS with Angular4 Development (Online)

08:00 PM - 09:30 PM IST(+5.30 GMT)

15 JUL
ASP.NET MVC with Angular4 Development (Online)

03:30 PM - 05:30 PM IST(+5.30 GMT)

1 JUL
ASP.NET MVC with Angular4 Development (Classroom)

08:00 AM - 09:30 AM IST

1 JUL
MEAN Stack 2 Development (Classroom)

11:00 AM - 12:30 PM IST(+5:30 GMT)

30 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

09:30 PM - 11:00PM IST(+5:30GMT)

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

20 MAY
NodeJS with Angular4 Development (Classroom)

05:00 PM - 07:00 PM

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+