Inheritance in Entity Framework

In previous tutorials, we created database tables based on domain classes. Also, we can design our domain classes using inheritance in EntityFramework. OOP techniques include "has a" and "is a" relationship whereas SQL-based relational has only a "has a" relationship among database tables. SQL database management doesn't support type inheritance. So, here we will learn how to map with domain class. There are three approaches to creating an inheritance hierarchy.
 
Table per Hierarchy (TPH): Inheritance is a hierarchical concept, in other words where one class is derived from another class. In TPH inheritance one database table stores the full data for all the entities in the inheritance hierarchy format.
 
Table per Type (TPT): This creates a separate table for each domain class or POCO class.
 
Table per Concrete class (TPC): This creates one table for one concrete class, but not for the abstract class. So when you inherit the abstract class in multiple concrete classes, then the properties of the abstract class will be part of each table of the concrete class.
 
We will understand Table per Hierarchy (TPH) with an example.
 
Step 1
 
Go to SQL Server Management Studio and execute the following query:
  1. Create Table Student  
  2. (  
  3.     ID int primary key identity,  
  4.     FirstName nvarchar(50),  
  5.     LastName nvarchar(50),  
  6.     Gender nvarchar(50),  
  7.     StudentSchoolName nvarchar(50),  
  8.     SchoolStudentclass nvarchar(50),  
  9.     StudentCollageName nvarchar(50),  
  10.     CollageStudentBranch nvarchar(50) Discriminator nvarchar(50)  
  11. )  
  12. Insert into Student values  
  13. (  
  14.     'Munesh''Sharma''Male'nullnull,  
  15.     'VIT''IT''CollageStudent'  
  16. )  
  17. Insert into Student values  
  18. (  
  19.     'Rahul''Sharma''Male''KVM''Seven',  
  20.     nullnull'SchoolStudent'  
  21. )  
  22. Insert into Student values  
  23. (  
  24.     'Sara''vilium''Female''Aadharsh',  
  25.     'Eight'nullnull'SchoolStudent'  
  26. )  
  27. Insert into Student values  
  28. (  
  29.     'Rani''hash''Female'nullnull,  
  30.     'MIT''ECE''CollageStudent'  
  31. )  
  32. Insert into Student values  
  33. (  
  34.     'XYZ''ABC''Female''Ravat''Tenth',  
  35.     nullnull'SchoolStudent'  
  36. )  
  37. Insert into Student values  
  38. (  
  39.     'Anshuman''EFG''Male'nullnull,  
  40.     'BTC''Mechenical''CollageStudent'  

 
Step 2
 
Right-click on your application and add a new item as “ADO.Net Entity Data Modal”. Then click Ok and select the Database First approach.
 
Provide your connection and select “Student” table. Here you will see only one entity, but we want 3 entities as in the following:
 
 
Step 3
 
Here in this entity Student common information (such as. ID, Firstname, Lasrname, Gender) will be an abstract class. And another entity such as “Address, mobile” and college, branch will inherit this abstract class.
 
Step 4
 
To do this using EF designer:
  1. Right-click on the designer surface and select Add New -> Entity
  2. At this screen you will provide your new entity name “Student Address info” and the base type will be your abstract entity. Cut Address and mobile from Base entity and paste it in the new entity, then your entity will look like the following:
 
Again right-click on the designer surface and do the same process for adding a new entity and name it “Student Study info”. Paste College and branch to this entity.
 
 
At this point you have 3 entities and these 2 new entities will be inherited from the Base Student entity.
 
 
Right-click on SchoolStudent and click on table Mapping and there map SchoolStudent Entity to Student Table. Also, provide the conditional mapping in which we are using the Discriminator column to determine that it is a School student or College student.
 
 
Again the same goes to CollageStudent entity in other words map this entity with the Student table and give the Discriminator condition.
 
 
Now when we compile this application it will give an error because we are using the Discriminator column on both sides so delete the Discriminator column from the Student entity.
 
Now finally right-click on the Student entity and click on properties and Set Abstract=true. This will make the Student class an abstract class.
  1. < div="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"> </ asp : GridView>  
  6. </ div>  
  7.   
  8. protected void Button1_Click(object sender, EventArgs e) {  
  9.     GridView1.DataSource = ConvertListToDataTable(  
  10.         studentDBContext.Students.ToList()  
  11.     );  
  12.     GridView1.DataBind();  
  13. }  
  14. protected void Button2_Click(object sender, EventArgs e) {  
  15.     GridView1.DataSource = studentDBContext.Students.OfType < CollageStudent > ().ToList();  
  16.     GridView1.DataBind();  
  17. }  
  18. protected void Button3_Click(object sender, EventArgs e) {  
  19.     GridView1.DataSource = studentDBContext.Students.OfType < SchoolStudent > ().ToList();  
  20.     GridView1.DataBind();  
  21. }  
  22. private DataTable ConvertListToDataTable(List < Student > students) {  
  23.     DataTable dt = new DataTable();  
  24.     dt.Columns.Add("ID");  
  25.     dt.Columns.Add("FirstName");  
  26.     dt.Columns.Add("LastName");  
  27.     dt.Columns.Add("Gender");  
  28.     dt.Columns.Add("SchoolStudentName");  
  29.     dt.Columns.Add("SchoolStudentClass");  
  30.     dt.Columns.Add("CollageStudentName");  
  31.     dt.Columns.Add("CollageStudentBranch");  
  32.     dt.Columns.Add("Type");  
  33.     foreach(Student _student in students) {  
  34.         DataRow dr = dt.NewRow();  
  35.         dr["ID"] = _student.ID;  
  36.         dr["FirstName"] = _student.FirstName;  
  37.         dr["LastName"] = _student.LastName;  
  38.         dr["Gender"] = _student.Gender;  
  39.         if (_student is CollageStudent) {  
  40.             dr["CollageStudentName"] = (  
  41.                 (CollageStudent) _student  
  42.             ).CollageStudentName;  
  43.             dr["CollageStudentBranch"] = (  
  44.                 (CollageStudent) _student  
  45.             ).CollageStudentBranch;  
  46.             dr["Type"] = "CollageStudent";  
  47.         } else {  
  48.             dr["SchoolStudentName "] = (  
  49.                 (SchoolStudent) _student  
  50.             ).SchoolStudentName;  
  51.             dr["SchoolStudentClass "] = (  
  52.                 (SchoolStudent) _student  
  53.             ).SchoolStudentBranch;  
  54.             dr["Type"] = "SchoolStudent";  
  55.         }  
  56.         dt.Rows.Add(dr);  
  57.     }  
  58.     return dt;  
  59. }  
  60. }  

Now run your application and check the data.
 
You can visit this article on my blog.