Saturday, June 21, 2014

Entity framework code first and inheritance- Table Per Type

Also see part-2 of this blog post- Entity Framework code first and Inheritance–Table per hierarchy
Recently I am using Entity Framework a lot. So I tried some of advance scenario and this post related to it only. In this post we will learn how entity framework handles inheritance.

In object oriented world, We all use Inheritance for reusability. We create some base classes and inherit those in child classes to facilitate code reuse and we don’t have to write again and again. In this post we will also have same kind of methodology. We are going to create a “Person” class and inherit this in to Employee and Customer class and we are going to see how entity framework code first handles this kind of inheritance by default. I’m going to use entity framework version 6.0 and Visual Studio 2013.

So what we are waiting for. Let’s get started. So create a console application from file menu new project.


Once you are done with creating a application. It’s time to add entity framework via nuget package.


Now we’re done with adding a nuget package it’s time to write classed that we have decided. Following a code for Person class.

public class Person 
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

Now we are going to create a Employee class which will be inherited from the Person class and have some onw properties.

public class Employee :Person
    public string Designation { get; set; }

Same way following is a code for Customer Class.

public class Customer: Person 
    public string PhoneNumber { get; set; }

Now it’s time to add a database.


Now I’ve added connection string in my app config file like following.

  <add connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\data\Blog\Samples\EntityInheritance\EntityInheritance\EntityInheritance.mdf;Integrated Security=True" name="MyConnectionString" providerName="System.Data.SqlClient"/>

Now it’s time to write a code for database context file. Following is a code for that.

public class EDataContext : DbContext
    public EDataContext() : base("MyConnectionString") 
    public IDbSet<Customer> Customers { get; set; }
    public IDbSet<Employee> Employees { get; set; }

Here you can see it’s very simple. There are two dbset properties for Employee and Customer. Now in my main I have written following code to list employee.

static void Main(string[] args)
    using (var context = new EDataContext())
        var employees = context.Employees.ToList();
        foreach (Employee employee in employees)

Now let’s run this application. Right now it will do nothing as we don’t have any data inserted in it. But let’s take a look of database and see what happened there.


Here you can see that it has created two tables for both entities so this called “Table Per Type” inheritance where it created a two tables for each type Customer and Employee. In future post we will see “Table per hierarchy” inheritance where we will have a single table for Customers and Employees.

Hope you like it. Stay tuned for more!!.


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews