Skip to main content

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!!.


Popular posts from this blog

How to create Rest API(Web API) with ASP.NET Core 1.0

Recently Microsoft has released ASP.NET Core 1.0 RC2, I am getting lots of request from readers that about creating Web API. So I thought it will be a good idea to write a blog post about how to create Rest API(Web API) with ASP.NET Core 1.0.

So let’s get started via creating an ASP.NET Core 1.0 Web Application like following.

Once you click on ASP.NET Web Application, It will ask whether you need to create Web Application or Web API application. We are going to Web API so I am going to select Web API Application like following. Please note that in ASP.NET Core 1.0 there is no separate libraries or DLLs required for creating web APIs. This is just a project template.

Now once you click on OK It will create a Web API application with default values controller and program.cs.  As you know Program.cs is now starting point for the ASP.NET Core 1.0 application so It contains all the required configuration and startup items. Following is a code for that.
using System.IO; using Microsoft.Asp…

How to use migration with Entity Framework Core

Entity framework core is  the lightweight, extensible and cross-platform version of Entity Framework. Before some time, Microsoft has Released a new version of Entity Framework RC2. I have written a couple of blog post about Entity framework code first migration earlier for Entity framework 6.0. So there was a couple of request coming for writing a blog post about Entity Framework Core RC2 migration. So I thought it will be a good idea to give an overview how database migration works in Entity Framework Core RC2. This post will cover a basic scenario where we are going to create the database with existing ASP.NET Identity migration and then we are going to create a new model and have that migration applied in the database.

How to use Entity Framework Migrations: Let’s get started, To demonstrate entity framework core migrations, I am going to create a sample core web application like following.

Once we select core application it will appear the following dialog.

Now w…

Solution: gulp is not recognized as internal or external command- Visual Studio

Recently, After Upgrading to the newer version of Gulp, I was getting this error.  After doing lots of  finding online and in my system I was able to figure out that Modules like Gulp does not installed to the path. So when you try to run it from the command line it was showing below error.
gulp is not recognized as internal or external command How to solve : gulp is not recognized as internal or external command To solve this error we just need to create a new environment variable and set the NPM modules path there.

To create an environment variable,  Right click My Computer/ This Pc and click on properties.  It will open following windows.

Go to Advance System Settings and it will open system properties windows like below.

Click on Environment Variables it will open an environment and system variables windows like below.

Click on new System variable and put NODE_PATH in system variable name and %AppData%\npm\node_modules into the variable value like below.

Click on Ok. That’s it. N…