Table Per Type (TPT) Hierarchy Insert Data Via EDM Framework

Introduction 

Today, in this article let's play around with an interesting and very useful concept in entity framework.

Question: What is Table Per Type Hierarchy?

In simple terms "The parent entity is associated with many child entities where the child entities can inherit the data from the parent. Here there is just a table that is subdivided into multiple entities based upon the properties".

Step 1: Create a new web application

Output1.jpg

Step 2: Entities in DB looks like this:

Output2.png

Step 3: Adding New Entity Data Model Framework

Output3.jpg

Step 4: Select the tables from DB

Output4.png

Step 5: Entity in Model Browser

Output5.png

Step 6: Add New Entity

Output6.png

Output7.png

Output8.png

Output9.png

Output10.png

Step 7: Make base type of parent entity as abstract

Output11.png

Step 8: Delete the property from parent entity and put it in the new entity

Output12.png

Output13.png

Step 9: Delete the unnecessary properties from the parent entity but make sure to specify up in the conditions during mappings

Output14.png
 

Step 10: Do the table mappings for the child entities

Output15.png
 

Output16.png
 

Output17.png

Step 11: Make nullable properties to false whereever it's required

Output18.png
 

Output19.png
 

Step 12: The complete code of WebForm1.aspx looks like this: 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TPTHiearchyInsertApp.WebForm1" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.         <center>  
  11.             <table>  
  12.                 <tr>  
  13.                     <td>  
  14.                         <asp:Label ID="Label1" runat="server" Text="Insert Data with TPT Hirearchy via EDM Framework"  
  15.                             Font-Bold="true"></asp:Label>  
  16.                     </td>  
  17.                 </tr>  
  18.             </table>  
  19.             <br />  
  20.             <br />  
  21.             <table>  
  22.                 <tr>  
  23.                     <td>  
  24.                         <asp:Label ID="Label2" runat="server" Text="Please Enter Transport Info: " ForeColor="Brown"  
  25.                             Font-Bold="true" Font-Italic="true"></asp:Label>  
  26.                     </td>  
  27.                     <td>  
  28.                         <asp:TextBox ID="TextBox1" runat="server" Width="117px"></asp:TextBox>  
  29.                     </td>  
  30.                 </tr>  
  31.                 <tr>  
  32.                     <td>  
  33.                         <asp:Label ID="Label4" runat="server" Text="Please Enter Transport Code: " ForeColor="Brown"  
  34.                             Font-Bold="true" Font-Italic="true"></asp:Label>  
  35.                     </td>  
  36.                     <td>  
  37.                         <asp:TextBox ID="TextBox2" runat="server" Width="117px"></asp:TextBox>  
  38.                     </td>  
  39.                 </tr>  
  40.                 <tr>  
  41.                     <td colspan="2" align="center">  
  42.                         <asp:Button ID="Button1" runat="server" Text="Insert Train Data" OnClick="Button1_Click"  
  43.                             BackColor="Orange" Font-Bold="true" /><br />  
  44.                         <br />  
  45.                     </td>  
  46.                 </tr>  
  47.                 <tr>  
  48.                     <td colspan="2" align="center">  
  49.                         <asp:Button ID="Button2" runat="server" Text="Insert Bus Data" OnClick="Button2_Click"  
  50.                             BackColor="Orange" Font-Bold="true" /><br />  
  51.                         <br />  
  52.                     </td>  
  53.                 </tr>  
  54.             </table>  
  55.             <br />  
  56.             <br />  
  57.             <table>  
  58.                 <tr>  
  59.                     <td colspan="3">  
  60.                         <asp:Label ID="Label5" runat="server" Font-Bold="true"></asp:Label>  
  61.                     </td>  
  62.                 </tr>  
  63.             </table>  
  64.         </center>  
  65.     </div>  
  66.     </form>  
  67. </body>  
  68. </html> 

Step 13: The complete code of WebForm1.aspx.cs looks like this:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;namespace TPTHiearchyInsertApp  
  7. {  
  8.     public partial class WebForm1 : System.Web.UI.Page  
  9.     {  
  10.         protected void Page_Load(object sender, EventArgs e)  
  11.         {  
  12.         }  
  13.         protected void Button1_Click(object sender, EventArgs e)  
  14.         {  
  15.             if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))  
  16.             {  
  17.                 Label5.Text = "Please Enter Some Values";  
  18.                 Label5.ForeColor = System.Drawing.Color.Red;  
  19.             }  
  20.             else  
  21.             {  
  22.                 Train objTrain = new Train()  
  23.                 {  
  24.                     TransportCode = TextBox1.Text, TrainInfo = TextBox2.Text };  
  25.                 objEntities.AddToTransport(objTrain);  
  26.                 objEntities.SaveChanges();  
  27.                 Label5.Text = "Train Details Inserted Sucessfully";  
  28.                 Label5.ForeColor = System.Drawing.Color.Green;  
  29.                 TextBox1.Text = string.Empty;  
  30.                 TextBox2.Text = string.Empty;  
  31.             }  
  32.         }  
  33.         protected void Button2_Click(object sender, EventArgs e)  
  34.         {  
  35.             if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))  
  36.             {  
  37.                 Label5.Text = "Please Enter Some Values";  
  38.                 Label5.ForeColor = System.Drawing.Color.Red;  
  39.             }  
  40.             else  
  41.             {  
  42.                 Bus objBus = new Bus()  
  43.                 {  
  44.                     TransportCode = TextBox1.Text, BusInfo = TextBox2.Text  
  45.                 }  
  46.                 ;  
  47.                 objEntities.Transport.AddObject(objBus);  
  48.                 objEntities.SaveChanges();  
  49.                 Label5.Text = "Bus Details Inserted Sucessfully";  
  50.                 Label5.ForeColor = System.Drawing.Color.Green;  
  51.                 TextBox1.Text = string.Empty;  
  52.                 TextBox2.Text = string.Empty;  
  53.             }  
  54.         }  
  55.         #region Instance MembersCompanyEntities objEntities = new CompanyEntities();  
  56.         #endregion  
  57.     }  
  58. }   

Step 14: The output of the application looks like this:

Output20.png

Step 15: The inserted data output of the application looks like this:

Output21.png

Step 16: The data inserted into parent table looks like this:

Output22.png

We hope this article was useful for you.