This article explains
Table Per Hierarchy (TPH) Inheritance in the Code First approach. First go through the article
TPH With DataBase Approach.
Step 1
Go to your application and add a class (
Student.cs) file to your application. Also make this class an Abstract class.
- namespace EntityInheritance {
- public abstract class Student {
- public int ID {
- get;
- set;
- }
- public string FirstName {
- get;
- set;
- }
- public string LastName {
- get;
- set;
- }
- public string Gender {
- get;
- set;
- }
- }
- }
Step 2
Add another class file to the project and name it
CollageStudent.cs. Write the following code snippet:
- namespace EntityInheritance {
- public class CollageStudent: Student {
- public string StudentCollageName {
- get;
- set;
- }
- public string StudentCollageBranch {
- get;
- set;
- }
- }
- }
Step 3
Add one more class file to the project from School student and name it
SchoolStudent.cs.
- namespace EntityInheritance {
- public class SchoolStudent: Student {
- public string StudentSchoolName {
- get;
- set;
- }
- public string StudentSchoolclass {
- get;
- set;
- }
- }
- }
Step 4
Add a class file for DBContext. Name it
StudentDBContext.cs and write the following code:
- using System.Data.Entity;
- namespace EntityInheritance {
- public class StudentDBContext: DbContext {
- public DbSet < Student > Students {
- get;
- set;
- }
- }
- }
Step 5
Add the database connection string in the
web.config file.
- <connectionStrings>
- <add name="StudentDBContext"
- connectionString="server=.; database=EntityInheritance; integrated security=SSPI;"
- providerName="System.Data.SqlClient" />
- </connectionStrings>
Step 6
Add a Webform to the project and write the following code:
- <div style="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>
- <asp:Button ID="btnAddcollageStudent" runat="server"
- Text="Add Collage student" onclick=" btnAddcollageStudent _Click" />
- <br />
- <br />
- <asp:Button ID=" btnAddSchoolStudent " runat="server"
- Text=" Add School Student " onclick=" btnAddSchoolStudent _Click" />
- </div>
Step 7
Copy the following code in the code-behind file.
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- namespace EntityInheritances {
- public partial class WebForm1: System.Web.UI.Page {
- protected void Button1_Click(object sender, EventArgs e) {
- GridView1.DataSource = ConvertListToDataTable(
- studentDBContext.Students.ToList());
- GridView1.DataBind();
- }
- protected void Button2_Click(object sender, EventArgs e) {
- GridView1.DataSource = studentDBContext.Students.OfType < CollageStudent > ().ToList();
- GridView1.DataBind();
- }
- protected void Button3_Click(object sender, EventArgs e) {
- GridView1.DataSource = studentDBContext.Students.OfType < SchoolStudent > ().ToList();
- GridView1.DataBind();
- }
- private DataTable ConvertListToDataTable(List < Student > students) {
- DataTable dt = new DataTable();
- dt.Columns.Add("ID");
- dt.Columns.Add("FirstName");
- dt.Columns.Add("LastName");
- dt.Columns.Add("Gender");
- dt.Columns.Add("SchoolStudentName");
- dt.Columns.Add("SchoolStudentClass");
- dt.Columns.Add("CollageStudentName");
- dt.Columns.Add("CollageStudentBranch");
- dt.Columns.Add("Type");
- foreach(Student _student in students) {
- DataRow dr = dt.NewRow();
- dr["ID"] = _student.ID;
- dr["FirstName"] = _student.FirstName;
- dr["LastName"] = _student.LastName;
- dr["Gender"] = _student.Gender;
- if (_student is CollageStudent) {
- dr["CollageStudentName"] = ((CollageStudent) _student).CollageStudentName;
- dr["CollageStudentBranch"] = ((CollageStudent) _student).CollageStudentBranch;
- dr["Type"] = "CollageStudent";
- } else {
- dr["SchoolStudentName "] = ((SchoolStudent) _student).SchoolStudentName;
- dr["SchoolStudentClass "] = ((SchoolStudent) _student).SchoolStudentBranch;
- dr["Type"] = "SchoolStudent";
- }
- dt.Rows.Add(dr);
- }
- return dt;
- }
- }
- }
- protected void btnAddCollageStudent_Click(object sender, EventArgs e) {
- CollageStudent collagestudent = new CollageStudent {
- FirstName = " Munesh",
- LastName = "Sharma",
- Gender = "Male"
- CollageStudentName = "VIT"
- CollageStudentBranch = "IT"
- };
- StudentDBContext studentDB = new StudentDBContext();
- studentDB.Students.Add(collagestudent);
- studentDB.SaveChanges();
- }
- protected void btnAddSchoolStudent_Click(object sender, EventArgs e) {
- SchoolStudent schoolstudent = new SchoolStudent {
- FirstName = " Rahul",
- LastName = "Sharma",
- Gender = "Male"
- CollageStudentName = "KVM"
- CollageStudentBranch = "Seven"
- };
- StudentDBContext studentDB = new StudentDBContext();
- studentDB.Students.Add(schoolstudent);
- studentDB.SaveChanges();
- }
- }
- }
Step 8
First delete the “
EntityInheritance” database in SQL Server if you have it.
Step 9
Now run the application. At this time your database and table will be created.
Step 10
Copy and paste the following query.
- Insert into Student values ('Munesh', 'Sharma','Male',null,null,'VIT','IT','CollageStudent')
- Insert into Student values ('Rahul', 'Sharma','Male','KVM','Seven',null,null,'SchoolStudent')
- Insert into Student values ('Sara', 'vilium','Female','Aadharsh','Eight',null,null,'SchoolStudent')
- Insert into Student values ('Rani', 'hash','Female',null,null,'MIT','ECE','CollageStudent')
- Insert into Student values ('XYZ', 'ABC','Female','Ravat','Tenth',null,null,'SchoolStudent')
- Insert into Student values ('Anshuman', 'EFG','Male',null,null,'BTC','Mechenical','CollageStudent')
Note: At this time the
Discriminator column is created automatically based on the
type of Student and the information is inserted.
You can visit this article in my
blog.
Munesh SharmaPosted Jun 30, 2015, 10:50 PM
Thank you
Santhakumar MunuswamyPosted Jun 30, 2015, 2:29 PM
Thanks for nice one
Munesh SharmaPosted Jun 30, 2015, 4:48 AM
i prefer this class as abstract class because it contain only properties and there definition is coming from other classes .. typically used to define a base class in the hierarchy. What's special about them, is that you can't create an instance of them - if you try, you will get a compile error.
Gaurav GuptaPosted Jun 30, 2015, 4:24 AM
Can u please explain the reason behind to mark Student class as Abstract class?
Rahul Kumar SaxenaPosted Jun 30, 2015, 3:49 AM
good show