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 includes "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 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. Insert into Student  
  12. values  
  13. (  
  14. 'Munesh''Sharma''Male'nullnull,  
  15. 'VIT''IT''CollageStudent'  
  16. Insert into Student  
  17. values  
  18. (  
  19. 'Rahul''Sharma''Male''KVM''Seven',  
  20. nullnull'SchoolStudent'  
  21. Insert into Student  
  22. values  
  23. (  
  24. 'Sara''vilium''Female''Aadharsh',  
  25. 'Eight'nullnull'SchoolStudent'  
  26. Insert into Student  
  27. values  
  28. (  
  29. 'Rani''hash''Female'nullnull,  
  30. 'MIT''ECE''CollageStudent'  
  31. Insert into Student  
  32. values  
  33. (  
  34. 'XYZ''ABC''Female''Ravat''Tenth',  
  35. nullnull'SchoolStudent'  
  36. Insert into Student  
  37. 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 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 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 Discriminator condition.
 
 
Now when we compile this application it will give an error because we are using the Discriminator column both side 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" > < asp : Button ID = " Button1" runat = "server" Text = "All Student Information" onclick = "Button1_Click" /> < asp : Button ID = " Button2" runat = "server" Text = "Collage Student Information" onclick = "Button2_Click" /> < asp : Button ID = " Button3" runat = "server" Text = "School Student Information" onclick = "Button3_Click" /> < asp : GridView ID = "GridView1" runat = "server" > </ asp : GridView > </ div > protected void Button1_Click(object sender, EventArgs e) { GridView1.DataSource = ConvertListToDataTable(  
  2.   studentDBContext.Students.ToList()  
  3. );  
  4. GridView1.DataBind();  
  5. } protected void Button2_Click(object sender, EventArgs e) { GridView1.DataSource = studentDBContext.Students.OfType < CollageStudent >().ToList();  
  6. GridView1.DataBind();  
  7. } protected void Button3_Click(object sender, EventArgs e) { GridView1.DataSource = studentDBContext.Students.OfType < SchoolStudent >().ToList();  
  8. GridView1.DataBind();  
  9. } private DataTable ConvertListToDataTable (List < Student > students)   
  10. { DataTable dt = new DataTable();  
  11. dt.Columns.Add("ID");  
  12. dt.Columns.Add("FirstName");  
  13. dt.Columns.Add("LastName");  
  14. dt.Columns.Add("Gender");  
  15. dt.Columns.Add("SchoolStudentName");  
  16. dt.Columns.Add("SchoolStudentClass");  
  17. dt.Columns.Add("CollageStudentName");  
  18. dt.Columns.Add("CollageStudentBranch");  
  19. dt.Columns.Add("Type");  
  20. foreach (Student _student in students) { DataRow dr = dt.NewRow();  
  21. dr[ "ID" ] = _student.ID;  
  22. dr[ "FirstName" ] = _student.FirstName;  
  23. dr[ "LastName" ] = _student.LastName;  
  24. dr[ "Gender" ] = _student.Gender;  
  25. if (_student is CollageStudent) { dr[ "CollageStudentName" ] = (  
  26.   (CollageStudent) _student  
  27. ).CollageStudentName;  
  28. dr[ "CollageStudentBranch" ] = (  
  29.   (CollageStudent) _student  
  30. ).CollageStudentBranch;  
  31. dr[ "Type" ] = "CollageStudent";  
  32. else { dr[ "SchoolStudentName " ] = (  
  33.   (SchoolStudent) _student  
  34. ).SchoolStudentName;  
  35. dr[ "SchoolStudentClass " ] = (  
  36.   (SchoolStudent) _student  
  37. ).SchoolStudentBranch;  
  38. dr[ "Type" ] = "SchoolStudent";  
  39. } dt.Rows.Add(dr);  
  40. return dt;  
  41. } } } 
Now run your application and check the data.
You can visit this article in my blog.