Table Per Type Inheritance in Entity Framework

The concept of Table Per Type (TPT) is that, for every entity it will create a new table. For example, in the following screen there are 3 entities (Student, College Student, School Student). It will create 3 new tables for each entity.

inheritance

In Table Per Hierarchy we saw that one database table stores all the data for all the entity types.

With this there is the problem that when we store Collage student entity data into the table then the columns “CollageStudentName” and “CollageStudentbranch” will be left NULL and the same goes to School student.

For removing this problem we use Table Per Type inheritance. In this we will create 3 tables for each entity.

The 3 tables are named “Student table”, “CollageStudent Table” and “SchoolStudent table”. The common thing in all the tables is that “StudentID” will be the same.

This StudentID will be the primary key in Student table and will be the foreign key in the CollageStudent and SchoolStudent tables.
  1. Create Table Student  
  2. (  
  3. StudentID int primary key,  
  4. FirstName nvarchar(50),  
  5. LastName nvarchar(50),  
  6. Gender nvarchar(50),  
  7. )  
  8. GO  
  9. Create Table CollageStudents  
  10. (  
  11. StudentID int foreign key references  
  12. Student(StudentID) not null,  
  13. CollageName nvarchar(50),  
  14. CollageBranch nvarchar(50)  
  15. )  
  16. GO  
  17. Create Table SchoolStudents  
  18. (  
  19. StudentID int foreign key references  
  20. Student(StudentID) not null,  
  21. SchoolName nvarchar(50),  
  22. SchoolClass nvarchar(50)  
  23. )  
  24. GO 
  1. Insert into Student values (1, 'Munesh''Sharma','Male')  
  2. Insert into Student values (2, 'Rahul''Sharma','Male')  
  3. Insert into Student values (3, 'Sara''vilium','Female')  
  4. Insert into Student values (4, 'Rani''hash','Female')  
  5. Insert into Student values (5, 'XYZ''ABC','Female')  
  6. Insert into Student values (6, 'Anshuman''EFG','Male')  
  7. Insert into CollageStudents values (1, 'VIT','IT')  
  8. Insert into CollageStudents values (4, 'MIT','ECE')  
  9. Insert into CollageStudents values (6, 'BTC','Mechenical')  
  10. Insert into SchoolStudents values (2, 'KVM','Seven')  
  11. Insert into SchoolStudents values (3,'Aadharsh','Eight')  
  12. Insert into SchoolStudents values (5, 'Ravat','Tenth'

Now go to your application and right-click on the Solution Explorer and select ADO.NET Entity Data Model and select your table and generate the entity. The following 3 entities will be generated:

generate Entity

Now delete the association between the Student entity and the CollageStudent entity and this will also automatically delete the CollageStudent navigation property from the student entity and the student navigation property from the CollageStudent entity.

Do the same thing with the SchoolStudent entity.

SchoolStudent entity

Now add the inheritance relationship between the Student entity and the collageStudent entity.

  1. Right-click on the designer surface and click on the Add-Inheritance option.

  2. Select Student at the base entity and collageStudent as the derived entity.

    add new inheritance

  3. When you click on Inheritance, your screen will look as in the following. Add the base entity and derived entity using a drop down as in the following:

    add inheritance

  4. Do the same with the Student entity and SchoolStudent entities. This means generate inheritance for these entities.

    click ok

  5. After creating an inheritance between these entities your entity model will look as in the following:

    entity modal

  6. Let's understand this with an example.
  1. < div style = "font-family: Arial" > < asp: Button ID = " Button1"  
  2. runat = "server"  
  3. Text = "All Student Information"  
  4. onclick = "Button1_Click" / > < asp: Button ID = " Button2"  
  5. runat = "server"  
  6. Text = "Collage Student Information"  
  7. onclick = "Button2_Click" / >  
  8.   
  9. < asp: Button ID = " Button3"  
  10. runat = "server"  
  11. Text = "School Student Information"  
  12. onclick = "Button3_Click" / >  
  13.   
  14. < asp: GridView ID = "GridView1"  
  15. runat = "server" > < /asp:GridView>  
  16. </div >  
  17.   
  18. protected void Button1_Click(object sender, EventArgs e) {  
  19.     GridView1.DataSource = ConvertListToDataTable(  
  20.     studentDBContext.Students.ToList());  
  21.     GridView1.DataBind();  
  22.   
  23.   
  24. }  
  25. protected void Button2_Click(object sender, EventArgs e) {  
  26.     GridView1.DataSource = studentDBContext.Students.OfType < CollageStudent > ().ToList();  
  27.     GridView1.DataBind();  
  28.   
  29.   
  30. }  
  31. protected void Button3_Click(object sender, EventArgs e) {  
  32.     GridView1.DataSource = studentDBContext.Students.OfType < SchoolStudent > ().ToList();  
  33.     GridView1.DataBind();  
  34.   
  35.   
  36. }  
  37.   
  38.   
  39. private DataTable ConvertListToDataTable(List < Student > students) {  
  40.     DataTable dt = new DataTable();  
  41.     dt.Columns.Add("ID");  
  42.     dt.Columns.Add("FirstName");  
  43.     dt.Columns.Add("LastName");  
  44.     dt.Columns.Add("Gender");  
  45.     dt.Columns.Add("SchoolName");  
  46.     dt.Columns.Add("SchoolClass");  
  47.     dt.Columns.Add("CollageName");  
  48.     dt.Columns.Add("CollageBranch");  
  49.     dt.Columns.Add("Type");  
  50.   
  51.     foreach(Student _student in students) {  
  52.         DataRow dr = dt.NewRow();  
  53.         dr["ID"] = _student.ID;  
  54.         dr["FirstName"] = _student.FirstName;  
  55.         dr["LastName"] = _student.LastName;  
  56.         dr["Gender"] = _student.Gender;  
  57.   
  58.         if (_student is CollageStudent) {  
  59.             dr["CollageName"] = ((CollageStudent) _student).CollageStudentName;  
  60.             dr["CollageBranch"] = ((CollageStudent) _student).CollageStudentBranch;  
  61.   
  62.   
  63.             dr["Type"] = "CollageStudent";  
  64.         } else {  
  65.             dr["SchoolName "] = ((SchoolStudent) _student).SchoolStudentName;  
  66.             dr["SchoolClass "] = ((SchoolStudent) _student).SchoolStudentBranch;  
  67.             dr["Type"] = "SchoolStudent";  
  68.         }  
  69.         dt.Rows.Add(dr);  
  70.     }  
  71.   
  72.     return dt;  

Run your application and see the output.

Up Next
    Ebook Download
    View all
    Learn
    View all