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 the 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">  
    2.     <asp: Button ID=" Button1" runat="server" Text="All Student Information" onclick="Button1_Click" />  
    3.     <asp: Button ID=" Button2" runat="server" Text="Collage Student Information" onclick="Button2_Click" />  
    4.     <asp: Button ID=" Button3" runat="server" Text="School Student Information" onclick="Button3_Click" />  
    5.     <asp: GridView ID="GridView1" runat="server">  
    6.         </asp:GridView>  
    7. </div>  
    8. protected void Button1_Click(object sender, EventArgs e) {  
    9.     GridView1.DataSource = ConvertListToDataTable(  
    10.         studentDBContext.Students.ToList());  
    11.     GridView1.DataBind();  
    12. }  
    13. protected void Button2_Click(object sender, EventArgs e) {  
    14.     GridView1.DataSource = studentDBContext.Students.OfType < CollageStudent > ().ToList();  
    15.     GridView1.DataBind();  
    16. }  
    17. protected void Button3_Click(object sender, EventArgs e) {  
    18.     GridView1.DataSource = studentDBContext.Students.OfType < SchoolStudent > ().ToList();  
    19.     GridView1.DataBind();  
    20. }  
    21. private DataTable ConvertListToDataTable(List < Student > students) {  
    22.         DataTable dt = new DataTable();  
    23.         dt.Columns.Add("ID");  
    24.         dt.Columns.Add("FirstName");  
    25.         dt.Columns.Add("LastName");  
    26.         dt.Columns.Add("Gender");  
    27.         dt.Columns.Add("SchoolName");  
    28.         dt.Columns.Add("SchoolClass");  
    29.         dt.Columns.Add("CollageName");  
    30.         dt.Columns.Add("CollageBranch");  
    31.         dt.Columns.Add("Type");  
    32.         foreach(Student _student in students) {  
    33.             DataRow dr = dt.NewRow();  
    34.             dr["ID"] = _student.ID;  
    35.             dr["FirstName"] = _student.FirstName;  
    36.             dr["LastName"] = _student.LastName;  
    37.             dr["Gender"] = _student.Gender;  
    38.             if (_student is CollageStudent) {  
    39.                 dr["CollageName"] = ((CollageStudent) _student).CollageStudentName;  
    40.                 dr["CollageBranch"] = ((CollageStudent) _student).CollageStudentBranch;  
    41.                 dr["Type"] = "CollageStudent";  
    42.             } else {  
    43.                 dr["SchoolName "] = ((SchoolStudent) _student).SchoolStudentName;  
    44.                 dr["SchoolClass "] = ((SchoolStudent) _student).SchoolStudentBranch;  
    45.                 dr["Type"] = "SchoolStudent";  
    46.             }  
    47.             dt.Rows.Add(dr);  
    48.         }  
    49.         return dt; 
      Run your application and see the output.


      Similar Articles