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:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TPTHiearchyInsertApp.WebForm1" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <center>

            <table>

                <tr>

                    <td>

                        <asp:Label ID="Label1" runat="server" Text="Insert Data with TPT Hirearchy via EDM Framework"

                            Font-Bold="true"></asp:Label>

                    </td>

                </tr>

            </table>

            <br />

            <br />

            <table>

                <tr>

                    <td>

                        <asp:Label ID="Label2" runat="server" Text="Please Enter Transport Info: " ForeColor="Brown"

                            Font-Bold="true" Font-Italic="true"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox1" runat="server" Width="117px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label4" runat="server" Text="Please Enter Transport Code: " ForeColor="Brown"

                            Font-Bold="true" Font-Italic="true"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox2" runat="server" Width="117px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Button ID="Button1" runat="server" Text="Insert Train Data" OnClick="Button1_Click"

                            BackColor="Orange" Font-Bold="true" /><br />

                        <br />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Button ID="Button2" runat="server" Text="Insert Bus Data" OnClick="Button2_Click"

                            BackColor="Orange" Font-Bold="true" /><br />

                        <br />

                    </td>

                </tr>

            </table>

            <br />

            <br />

            <table>

                <tr>

                    <td colspan="3">

                        <asp:Label ID="Label5" runat="server" Font-Bold="true"></asp:Label>

                    </td>

                </tr>

            </table>

        </center>

    </div>

    </form>

</body>

</html>
 

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

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;namespace TPTHiearchyInsertApp

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))

            {

                Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else

            {

                Train objTrain = new Train()

                {

                    TransportCode = TextBox1.Text, TrainInfo = TextBox2.Text };

                objEntities.AddToTransport(objTrain);

                objEntities.SaveChanges();

                Label5.Text = "Train Details Inserted Sucessfully";

                Label5.ForeColor = System.Drawing.Color.Green;

                TextBox1.Text = string.Empty;

                TextBox2.Text = string.Empty;

            }

        }

        protected void Button2_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))

            {

                Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else

            {

                Bus objBus = new Bus()

                {

                    TransportCode = TextBox1.Text, BusInfo = TextBox2.Text

                }

                ;

                objEntities.Transport.AddObject(objBus);

                objEntities.SaveChanges();

                Label5.Text = "Bus Details Inserted Sucessfully";

                Label5.ForeColor = System.Drawing.Color.Green;

                TextBox1.Text = string.Empty;

                TextBox2.Text = string.Empty;

            }

        }

        #region Instance MembersCompanyEntities objEntities = new CompanyEntities();

        #endregion

    }

}
 

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.

 

Up Next
    Ebook Download
    View all
    Learn
    View all
    MVC Corporation is consulting and IT services based company.