Click here to Skip to main content
15,892,161 members
Articles / Programming Languages / C#

Inheritance in Entity Framework: Table Per Type

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
14 Jul 2014CPOL3 min read 28.5K   8   4
Inheritance in Entity Framework: Table per Type

Introduction

This is the Entity Framework article series. In our previous articles, we have learned various approaches of Entity Framework, Database initialization strategy using Entity framework, how to handle inheritance using Table per Hierarchy implementation, you can read them here:

In our previous article, we have discussed that there are three different implementations of inheritance in Entity Framework, and those are:

  • Table per hierarchy
  • Table per type
  • Table per concrete type

In our previous article, we implemented Table per Hierarchy; in this article, we will learn Table per Type implementation. The concept of table per type is, Table will get created for each and every type. For example, if there is class relationship like this:

Image 1

Then for all three classes, three different tables will get created. Let’s take a look at an example. Have a look at the following code:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    public class Person
    {
        [Key]
        public int PersonId { get; set; }
        [MaxLength(10)]
        public string name { get; set; }
        public string surname { get; set; }
    }
    [Table("Friend")]
    public class Friend : Person 
    {
        public Boolean IsClassmate { get; set; }
    }

    [Table("BestFriend")]
    public class BestFriend : Person
    {
        public string Address { get; set; }
    }
    
    public class personContext : DbContext
    {
        public personContext()
            : base("DBConnectionString")
        {
        }
        public DbSet<person> Person { get; set; }
        public DbSet<friend> Friend { get; set; }
        public DbSet<bestfriend> BestFriend { get; set; }
    }
}

The implementation is very simple to understand. We have used Data annotation technique to map relationship between entity and relational database. Friend and BestFriend class are derived from Person class. Now, point to discuss, to implement Table per type, we just have to add “Table” attribute above the class definition, that’s all and Entity Framework will create a separate table for each class. In hierarchical relationship, it’s not necessary to decorate base class with “Table” attribute. Because of that, we did not add in this example and even those lines are too optional. It’s enough to specify the base type.

C#
public DbSet<Friend> Friend { get; set; }
public DbSet<BestFriend> BestFriend { get; set; }

Once you run the code and seed database, you will find the below database structure, here we are seeing three tables for three different classes.

Image 2

Save Data in Three Tables

Now, we will try to save data in those tables and we will observe how data gets saved in tables. Have a look at the below code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApp.Company;
using System.Collections;

namespace ConsoleApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var ctx = new personContext())
            {
                //ctx.Database.Create();

                //Save Person Object
                ctx.Person.Add(new Person { name = "sourav", surname = "kayal" });
                ctx.SaveChanges();

                //Save Friend Object
                ctx.Friend.Add(new Friend { name = "foo", surname = "foo", IsClassmate = false });
                ctx.SaveChanges();

                //Save BestFriend Object
                ctx.BestFriend.Add(new BestFriend { name = "Ram", surname = "Shyam", Address = "Kolkata" });
                ctx.SaveChanges();
            }
        }
    }
}

The code is pretty simple to understand. If we run the code and open database, we will see that the data got saved in this structure in three different tables.

Image 3

Fine, we have seen how data gets saved in database, now we will retrieve data in various forms.

Retrieve “Friend” Object using query

We will fetch only “Friend” object from Person table, please try to remember that Person is the base table in this hierarchy. We can use OfType in LINQ to access only property of Person object. Here is a sample example.

C#
public static void Main(string[] args)
       {
           using (var ctx = new personContext())
           {
               var data = (from m in ctx.Person.OfType<friend>() select m).ToList();
               Console.WriteLine("Name :" + data[0].name);
               Console.WriteLine("Surname :" + data[0].surname);
               Console.WriteLine("Is Classmate :" + data[0].IsClassmate);
               Console.WriteLine("Person Id :" + data[0].PersonId);
           }
       }

Image 4

Retrieve BestFriend Object

This is similar to the previous one, if we want only object of “BestFriend”, then we can pass the parameter through OfType.

C#
public static void Main(string[] args)
        {
            using (var ctx = new personContext())
            {             
                var data = (from m in ctx.Person.OfType<bestfriend>() select m).ToList();
                Console.WriteLine("Name :" + data[0].name);
                Console.WriteLine("Surname :" + data[0].surname);
                Console.WriteLine("Address :" + data[0].Address);
                Console.WriteLine("Person Id :" + data[0].PersonId);                
            }
        }

And here is the output of this example:

Image 5

Retrieve Person Object

Now, if we want to access base class object, we need not specify anything in query. In this example, we will retrieve object of person class.

C#
public static void Main(string[] args)
        {
            using (var ctx = new personContext())
            {
                var data = (from m in ctx.Person select m).ToList();
                Console.WriteLine("Name :" + data[0].name);
                Console.WriteLine("Surname :" + data[0].surname);
                Console.WriteLine("Person Id :" + data[0].PersonId);                
            }
        }

And only we are getting the property of person class.

Image 6

Border Line

In this article, we have learned the concept of Table per Type implementation to implement inheritance in Entity Framework, hope you have got the fundamental idea. In our following article, we will discuss the concept of “Table per concrete type”.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer DELL International
India India
I am software developer from INDIA. Beside my day to day development work, i like to learn new technologies to update myself. I am passionate blogger and author in various technical community including dotnetfunda.com , c-sharpcorner.com and codeproject. My area of interest is modern web technology in Microsoft stack. Visit to my personal blog here.

http://ctrlcvprogrammer.blogspot.in/

Comments and Discussions

 
QuestionEF Table per Type code first Pin
Member 1247296520-Apr-16 1:25
Member 1247296520-Apr-16 1:25 
Questioncase is wrong. Pin
Steven Cramer8-Feb-16 17:28
Steven Cramer8-Feb-16 17:28 
GeneralYou always rock !! Pin
Sandeep Singh Shekhawat16-Jul-14 22:57
professionalSandeep Singh Shekhawat16-Jul-14 22:57 
GeneralRe: You always rock !! Pin
Sourav Kayal18-Jul-14 8:57
Sourav Kayal18-Jul-14 8:57 
Dear sandeep, Thanks for your valuable suggestion. I should give. The reality is, i use same project in many demos and articles, but surely i will change by bad habit.
ctrlcvprogrammer.blogspot.com

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.