Inheritance With EntityFramework

Dheeraj Kumar Gunti

Reading Time : ( words)

Hello All, Welcome back to Knowledge Base

Today we are going to know about inheritance in Entityframework.There are multiple ways of acheiveing inheritance in entity framework.

  • Table Per Hierarchy (TPH)
  • Table Per Type (TPT)

In this example we will see how to implement Table Per Hierarchy Inheritance.

TPH inheritance uses one database table to maintain data for all of the entity types in an inheritance hierarchy.

When we are targetting different entity types which holds specific fields which are unique across entities then we can go to have this concept applied on those entities.

Let's say we have three entities Person, Instructor and Student as below.

Here, we see we want FirstName and LastName as common properties which are important for both Instructor and Student.

When we apply inheritance on the above entities, respecitive properties can be accessible in the derived classes.

So here, Person is the base class which holds FirstName and LastName Properties. Instructor and Student are derived class which has extended properties of their own where an Instructor will have an HireDate and for a Student he will have an enrollment date. When we create inheritance using TPH, then both the entities data will be stored in one table with a discriminator.

Let's create a small project. Create three entities and specify the relationship as above.

Create a DbContext and specify the DbSets.

public class SchoolContext : DbContext
{
    public DbSet<Person> People { get; set; }
    public DbSet<Instructor> Insructors { get; set; }
    public DbSet<Student> Students { get; set; }
    public SchoolContext()
    : base("DefaultConnection")
    {
    }
}

Enable Migrations on the project.

Add Migration on the project.

Migrations have been created in the project.



CreateTable(
    "dbo.People",
                c => new
    {
                            PersonId = c.Int(nullable: false, identity: true),
                            FirstName = c.String(),
                            LastName = c.String(),
                            HireDate = c.DateTime(),
                            EnrollmentDate = c.DateTime(),
                            Discriminator = c.String(nullable: false, maxLength: 128),
    })
.PrimaryKey(t => t.PersonId);

We can see Discriminator property added in the migration file for Person Entity. Person Entity has included properties of Instructor, Student and created a single table.

Let's check the database.



Create Instructor Controller using Entity Framework template, Choose Instructor Model and SchoolContext. Scaffolding will be generated with all the templates. Run the application

When we try to create a new Instructor, template created in such a way that FirstName, LastName are taken from base class Person and Hire Date is taken from Instructor entity.

Check the database, Discriminator is updated with "Instructor".

When we try to create a new Student, template created in such a way that FirstName, LastName are taken from base class Person and Hire Date is taken from Instructor entity.

Check the database, Discriminator is updated with "Student".



Note: TPH is default in Entity framework.



Click here to Download the Sample