Wednesday, 15 October 2014

Entity framework code first (Table Per hierarchy inheritance)




Before this, I have posted another blog about Entity framework code first (Table Per Type inheritance) and this is next in the series.

Here you can see that it has created two tables for both entities so this called “Table Per Type” In

In previous post we have learned how entity framework code first handles inheritance to create tables per type and in this part we are going to extend this and modify code of data context to see how it creates a “Table per Hierarchy”.

We’re going to use same code as previous post just going to change DataContext code like below.

public class DataContext : DbContext
    {
        public DataContext() : base("MyConnectionString")
        {
        }
        public IDbSet<Student> Students { get; set; }
        public IDbSet<Employee> Employees { get; set; }
        public IDbSet<Person> Persons { get; set; }
    }

Here in above code we have just added one more class(Person) to IDbSet.
This will create a single table for inheritance hierarchy. Now if we run application again, we can see difference.














So this created a single table into DB, this is called "Table-Per-Hierarchy".

Stay tuned for more!!!

No comments:

Post a Comment