Table Per Type (TPT) Inheritance 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 Inheritance?

In simple terms "The parent entity is associated with many child entities where the child entities can inherit the data from the parent. Here the primary key of the parent entity is shared with the child entity that maintains a 1-1 relationship".

Step 1: Create a new web application

Output1.jpg

Step 2: Entities in the DB looks like this:

Output2.png

Step 3: Adding New Entity Data Model Framework

Output3.jpg

Step 4: Select the tables from the DB

Output4.png

Step 5: Entity in Model Browser

Output5.png

Step 6: Delete association links between entities

Output6.png

 

Step 7: Select the base types

Output7.png

Output8.png

Step 8: New inheritance associations established

Output9.png

Step 10: Delete the child table or entities Ids that will inherit from the parent entity

Output10.png

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

 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TPTInheritanceInsertEFApp.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 Inheritance 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="Label3" runat="server" Text="Please Select Transport Mode: " ForeColor="Brown"  
  34.                             Font-Bold="true" Font-Italic="true"></asp:Label>  
  35.                     </td>  
  36.                     <td>  
  37.                         <asp:DropDownList ID="DropDownList1" runat="server" Width="117px">  
  38.                             <asp:ListItem Text="Train" Value="0"></asp:ListItem>  
  39.                             <asp:ListItem Text="Bus" Value="1"></asp:ListItem>  
  40.                         </asp:DropDownList>  
  41.                     </td>  
  42.                 </tr>  
  43.                 <tr>  
  44.                     <td>  
  45.                         <asp:Label ID="Label4" runat="server" Text="Please Enter Transport Code: " ForeColor="Brown"  
  46.                             Font-Bold="true" Font-Italic="true"></asp:Label>  
  47.                     </td>  
  48.                     <td>  
  49.                         <asp:TextBox ID="TextBox2" runat="server" Width="117px"></asp:TextBox>  
  50.                     </td>  
  51.                 </tr>  
  52.                 <tr>  
  53.                     <td colspan="2" align="center">  
  54.                         <asp:Button ID="Button1" runat="server" Text="Insert Data" OnClick="Button1_Click"  
  55.                             BackColor="Orange" Font-Bold="true" /><br />  
  56.                         <br />  
  57.                     </td>  
  58.                 </tr>  
  59.             </table>  
  60.             <br />  
  61.             <br />  
  62.             <table>  
  63.                 <tr>  
  64.                     <td colspan="3">  
  65.                         <asp:Label ID="Label5" runat="server" Font-Bold="true"></asp:Label>  
  66.                     </td>  
  67.                 </tr>  
  68.             </table>  
  69.         </center>  
  70.     </div>  
  71.     </form>  
  72. </body>  
  73. </html> 

Step 12: 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 TPTInheritanceInsertEFApp  
  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.                 switch (int.Parse(DropDownList1.SelectedValue))  
  23.                 {  
  24.                     case 0:Train objTrain = new Train()  
  25.                     {  
  26.                         TransportMode = DropDownList1.SelectedItem.Text, TransportCode = TextBox2.Text, TrainInfo = TextBox1.Text };  
  27.                         objEntities.AddToTransport(objTrain);  
  28.                         objEntities.SaveChanges();  
  29.                         Label5.Text = "Train Details Inserted Sucessfully";  
  30.                         Label5.ForeColor = System.Drawing.Color.Green;  
  31.                         TextBox1.Text = string.Empty;  
  32.                         TextBox2.Text = string.Empty;  
  33.                         break;case 1:Bus objBus = new Bus()  
  34.                         {  
  35.                             TransportMode = DropDownList1.SelectedItem.Text, TransportCode = TextBox2.Text, BusInfo = TextBox1.Text  
  36.                         };  
  37.                         objEntities.Transport.AddObject(objBus);  
  38.                         objEntities.SaveChanges();  
  39.                         Label5.Text = "Bus Details Inserted Sucessfully";  
  40.                         Label5.ForeColor = System.Drawing.Color.Green;  
  41.                         TextBox1.Text = string.Empty;  
  42.                         TextBox2.Text = string.Empty;  
  43.                         break;  
  44.                 }  
  45.             }  
  46.         }  
  47.         #region Instance MembersCompanyEntities objEntities = new CompanyEntities();  
  48.         #endregion  
  49.     }  
  50. }   

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

Output11.png

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

Output12.png
 

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

Output13.png
 

Step 16: The data inserted in child entity output of the application looks like this:

Output14.png

I hope this article was useful for you.


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.