Delete Data - POCO Template

Introduction

This article demonstrates one of the interesting and very useful concepts in the Entity Framework.

Question: What POCO Template?

In simple terms "It enables code to be generated with a template based upon an Entity".

Step 1: Create a new web application

Output1.jpg

Step 2: Adding New Entity Data Model Framework

OutputEDF.jpg

Step 3: Add new POCO template

POCO Pic.jpg

Step 4: The complete code of WebForm1.aspx looks as in the following:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DeleteDataPOCOTemplate.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">

    <center>

        <div>

            <table>

                <tr>

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

                        <asp:Label ID="Label1" runat="server" Text="Delete Data - Complex Type App" Font-Bold="true"

                            Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label2" runat="server" Text="Please Enter Employee Id" ForeColor="Brown"

                            Font-Bold="true" Font-Size="Medium" Font-Names="Verdana"></asp:Label>

                    </td>

                    <td>

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

                    </td>

                </tr>

                <tr>

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

                        <asp:Button ID="Button1" runat="server" Text="Delete Data" Font-Names="Verdana" Width="213px"

                            BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />

                    </td>

                </tr>

                <tr>

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

                        <asp:Label ID="Label3" runat="server" Font-Bold="true" Font-Size="Medium" Font-Names="Verdana"></asp:Label>

                    </td>

                </tr>

            </table>

        </div>

    </center>

    </form>

</body>

</html>

Step 5: The complete code of WebForm1.aspx.cs looks as in the following:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;namespace DeleteDataPOCOTemplate

{

    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)){Label3.Text = "Please Enter Some Values";

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

            }

            else

            {

                int Id = int.Parse(TextBox1.Text);

                var query = (from r in objEntities.tblEmployee where r.EmpId == Id select r).Single();

                objEntities.DeleteObject(query);

                objEntities.SaveChanges();

                Label3.Text = "Data Deleted Successfully";

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

                TextBox1.Text = string.Empty;

            }

        }

        #region Instance MembersCompanyEntities objEntities = new CompanyEntities();

        #endregion

    }

}

Step 6: The output of the application looks as in the following:



Output2.png



Step 7: The deleted data output of the application looks as in the following:


Output3.png



I hope this article was useful for you.