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:
- Create Table Student
- (
- ID int primary key identity,
- FirstName nvarchar(50),
- LastName nvarchar(50),
- Gender nvarchar(50),
- StudentSchoolName nvarchar(50),
- SchoolStudentclass nvarchar(50),
- StudentCollageName nvarchar(50),
- CollageStudentBranch nvarchar(50) Discriminator nvarchar(50)
- ) 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'
- )
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:
- Right-click on the designer surface and select Add New -> Entity
- 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.
- < 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(
- 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;
- } } }
Now run your application and check the data.