Table Per Type (TPT) Inheritance Update 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 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: 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 9: Delete the child table or entities Ids that will inherit from the parent entity

Output10.png

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

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TPTInhertanceUpdateApp.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="Update 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="Update 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 11: 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 TPTInhertanceUpdateApp  
  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 = objEntities.Transport.OfType<Train>().SingleOrDefault(r => r.TransportCode.Equals(TextBox2.Text));  
  25.                         objTrain.TransportMode = DropDownList1.SelectedItem.Text;  
  26.                         objTrain.TrainInfo = TextBox1.Text;  
  27.                         objEntities.SaveChanges();  
  28.                         Label5.Text = "Train Details Updated Sucessfully";  
  29.                         Label5.ForeColor = System.Drawing.Color.Green;  
  30.                         TextBox1.Text = string.Empty;  
  31.                         TextBox2.Text = string.Empty;  
  32.                         break  
  33.                             ;  
  34.                     case 1:Bus objBus = objEntities.Transport.OfType<Bus>().SingleOrDefault(r => r.TransportCode.Equals(TextBox2.Text));  
  35.                         objBus.TransportMode = DropDownList1.SelectedItem.Text;  
  36.                         objBus.BusInfo = TextBox1.Text;  
  37.                         objEntities.SaveChanges();  
  38.                         Label5.Text = "Bus Details Updated Sucessfully";  
  39.                         Label5.ForeColor = System.Drawing.Color.Green;  
  40.                         TextBox1.Text = string.Empty;  
  41.                         TextBox2.Text = string.Empty;break;  
  42.                 }  
  43.             }  
  44.         }  
  45.         #region Instance MembersCompanyEntities objEntities = new CompanyEntities();  
  46.         #endregion  
  47.     }  
  48. }   

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

Output11.png

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

Output12.png

Step 14: The data updated in child entity output of the application looks like this:

Output13.png

I hope this article was useful for you.


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