EDM Framework CRUD Operations using DataContextObject


Question Arises: What is Entity Data Model Framework? 

In Simple Terms: "It's a Conceptual Data Model which will be acting as a bridge between Database and .NET Framework. It clearly depicts the ORM (Object Relational Mapping) from the perspective of Database logics easily".

Question Arises: How Entity Data Model Framework differs from LINQ to SQL Classes?

S.No EDM Framework LINQ to SQL Classes
1 It Supports different kinds of databases. It supports on SQL Server.
2 From the perspective of development effort, it is fast enough. Slow than EDM Framework from perspective of development effort.
3 Maps multiple tables at a time. Maps only single table at a time.
4 Bit Complex for New Users. Simple and user friendly.
5 Supports Database Change Synchronization. Does not Supports Exactly.
6 Maps Tables, Stored Procs, Reporting Stuff and so on. Maps Tables only.

So, I think we all are now good to go and implement this highly useful concept.

Let's get ready to start it off.

Firstly, I have created a database in SQL Server 2008.

EDM Eight.png

Now, we are eager to fire off Visual Studio 2010.
 
Let's Create Some Web Application Project in Version .NET 4.0.
 
So, let's add a new Item to the Project.
 
So go data Tab and Click on ADO.NET Entity Data Model.
 
Where the Complete Screen looks like this.

EDM Nie.png


Click next, where The Screen looks like this, now click on generate from database and click next.

EDM Ten.png

Next The Screen looks like this where you need to pass on database name and One Main Important thing here to remember is DataContextObject Name. The Data Context Name here is: Candidate Entities.

So, the Complete Screen looks like this:

EDM Eleven.png

After Clicking Next, Select the table which you want to perform the operation. So the Screen here looks like this:

EDM Eleven.png

And Finally Click Finish, Which will create an ADO.NET-EDM Framework for you

Now, let's get Started with Actual Fun.

So Let's Create a New Web Form.

Now, let's perform an insert operation to database. So The Complete Code of WebForm1.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="EDM_Application.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>
 
    <style type ="text/css">
 
    .div
 
    {
         font-family:Cambria;
         }
     </style>
 
</head>
 <
body>
 
    <form id="form1" runat="server">
 
    <div class="div">
 
    <center>
 
    <table>
 
    <tr>
 
    <td>
 
    </td>
 
    <td>
 
        <asp:Label ID="Label5" runat="server" Text="Vijay's EDM Framework CRUD Operation" Font-Bold="true"></asp:Label>
 
    </td>
 
    </tr>
 
    <tr>
 
    <td>
 
        <asp:Label ID="Label1" runat="server" Text="Please Enter FirstName" ForeColor="Brown" Font-Italic="true" Font-Bold="true"></asp:Label>
 
    </td>
 
    <td>
 
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 
    </td>
 
    </tr>
 
      <tr>
 
    <td>
 
        <asp:Label ID="Label2" runat="server" Text="Please Enter LastName" Font-Italic="true" ForeColor="Brown" Font-Bold="true"></asp:Label>
 
    </td>
 
    <td>
 
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
 
    </td>
 
    </tr>
 
      <tr>
 
    <td>
 
        <asp:Label ID="Label3" runat="server" Text="Please Enter Age" Font-Italic="true" ForeColor="Brown" Font-Bold="true"></asp:Label>
 
    </td>
 
    <td>
 
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
 
    </td>
 
    </tr>
 
    <tr>
 
    <td></td>
 
    <td>
 
        <asp:Button ID="Button1" runat="server" Text="Insert Values Here"
      onclick="Button1_Click" />
       
</td>
 
    </tr>
 
    </table>
 
    <table>
 
    <tr>
 
    <td>
     </
td>
 
    <td>
 
     <asp:Label ID="Label4" runat="server" Visible="true" ForeColor="Brown"></asp:Label>
 
    </td>
 
    </tr>
 
    </table>
 
    </center>
 
     </div>
 
    </form>
 
</body>
 </
html>

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 EDM_Application
 {
     public partial class WebForm1 : System.Web.UI.Page
     {
         protected void Page_Load(object sender, EventArgs e)
         {
             TextBox1.Focus();
             }
         protected void Insert()
         {
             var objEntities = new CandidateEntities();
             try
             {
                 if(TextBox1.Text==""|| TextBox2.Text==""||TextBox3.Text=="")
                 {
                     Label4.Text = "<b>Please Enter Some Values";
                     Label4.Visible = true;
                     }
                 else
                {
                     var objPerson = new Person();
                     objPerson.FirstName = TextBox1.Text;
                     objPerson.LastName = TextBox2.Text;
                     objPerson.Age = Convert.ToInt32(TextBox3.Text);
                     objEntities.AddToPerson(objPerson);
                     objEntities.SaveChanges();
                     TextBox1.Text = "";
                     TextBox2.Text = "";
                     TextBox3.Text = "";
                     Response.Redirect("~/WebForm2.aspx");
                     }
                 }
  catch (Exception exception)            
  {
  
                      exception.Message.ToString();
                }
         }
         protected void Button1_Click(object sender, EventArgs e)
         {
             Insert();
             }
         }
     } 

Now, after user is inserted values, let's create a new web form again where the page needs to be redirected after insertion. So, here we are going to perform deletion, retrieval and modification operations. 

So the Complete Code of WebForm2.aspx looks like this: 

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="EDM_Application.WebForm2" %> 
<!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>
    <style type="text/css">
        .div
        {
            font-family: Cambria;
        }
    </style>
</head>
<
body>
    <form id="form1" runat="server">
    <div class="div">
        <center>
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" AutoGenerateColumns="False"

AllowSorting="True" DataKeyNames="Id"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
GridLines="None" onpageindexchanging="GridView1_PageIndexChanging"
onrowdeleting="GridView1_RowDeleting" >
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />

<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
<Columns>
<
asp:BoundField HeaderText="Id" DataField="Id" SortExpression="Id" ReadOnly="true" />
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<%#Eval("FirstName") %>
</ItemTemplate>
<
EditItemTemplate>
<
asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
</
asp:TemplateField>
<
asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<%#Eval("LastName") %>
</ItemTemplate>
<
EditItemTemplate>
<
asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("LastName") %>'></asp:TextBox>
</EditItemTemplate>
</
asp:TemplateField>
<
asp:TemplateField HeaderText="Age">

<ItemTemplate>
<%#Eval("Age") %>
</ItemTemplate>
<
EditItemTemplate>
<
asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("Age") %>'></asp:TextBox>
</EditItemTemplate>
</
asp:TemplateField>
<
asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<
span onclick="return confirm('Are you Sure Want to Delete Data ')">
<asp:LinkButton ID="LinkButton3" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
</span>
</
ItemTemplate>
</
asp:TemplateField>
</
Columns>
</
asp:GridView>
<
table>
<
tr>
<
td></td>
<
td>
<
asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"> Click Here to Insert Data </asp:LinkButton>
</td>
</
tr>
</
table>
<
br />
<br />
<h1>Modify Data Here</h1>
<table>
<
tr>
<
td>
<
asp:Label ID="Label1" runat="server" Text="Please Enter Id"></asp:Label>
</td>
<
td>
<
asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</
tr>
<
tr>
<
td>
<
asp:Label ID="Label2" runat="server" Text="Please Enter FirstName"></asp:Label>
</td>
<
td>
<
asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</
tr>
<
tr>
<
td>
<
asp:Label ID="Label3" runat="server" Text="Please Enter LastName"></asp:Label>
</td>
<
td>
<
asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</
tr>
<
tr>
<
td>
<
asp:Label ID="Label4" runat="server" Text="Please Enter Age"></asp:Label>
</td>
<
td>
<
asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</td>
</
tr>
<
tr>
<
td></td>
<
td>
<
asp:Button ID="Button1" runat="server" Text="Click Here to Modify" onclick="Button1_Click" Width="119px" />
</td>
</
tr>
</
table><br />
<table>
<
tr>
<
td>
</
td>
<
td>
<
asp:Label ID="Label5" runat="server" Visible="false" ForeColor="Red" Font-Bold="true" Font-Size="Large">
</asp:Label>
</
td>
</
tr>
</
table>
</
center>
    </div>
    </form>
</body>
</
html>

The Complete Code of WebForm2.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 EDM_Application

{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Select();
            }
        }
        protected void Select()
        {
            var objEntities = new CandidateEntities();
            var query = from r in objEntities.Person select r;
            GridView1.DataSource = query; GridView1.DataBind();
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/WebForm1.aspx");
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Select();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                var objEntities = new CandidateEntities();
                var Id1 = GridView1.DataKeys[e.RowIndex].Value; var id = Convert.ToInt32(Id1);
                var query = objEntities.Person.Where(a => a.Id == id);
                if (query.Count() > 0)
                {
                    foreach (Person person in query)
                    {
                        objEntities.DeleteObject(person);
                    } objEntities.SaveChanges();
                } Select();
                Label5.Text = ("Records Deleted Successfully");
                Label5.Visible = true;
            }
            catch (ArgumentOutOfRangeException exception)
            {
                exception.Message.ToString();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
           try
            {
              if (TextBox4.Text == "" || TextBox5.Text == "" || TextBox6.Text == "" || TextBox7.Text == "")
                {
            Label5.Text = ("Please Enter Some Values");
                    Label5.Visible = true;
                }
                else
                {
                    var objEntities = new CandidateEntities();
                    var id1 = Convert.ToInt32(TextBox4.Text);
                    var person = objEntities.Person.Where(a => a.Id == id1).FirstOrDefault();
                    person.FirstName = TextBox5.Text;
                    person.LastName = TextBox6.Text;
                    person.Age = Convert.ToInt32(TextBox7.Text);
                    objEntities.SaveChanges();
                    Select();
                    TextBox4.Text = "";
                    TextBox5.Text = "";
                    TextBox6.Text = "";
                    TextBox7.Text = "";
                    Label5.Text = ("Records Updated Successfully");
                    Label5.Visible = true;
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
    }

} 

The Output of the WebForm1.aspx Application looks like this:

EDM First.png

The Insertion Operation of the Application looks like this:

EDM Second.png

The Output of the WebForm2.aspx Application looks like this:

EDM Third.png

The Output of Data Modification for the Application looks like this:

EDM Fourth.png

The Output of Data Update Application looks like this:

EDM Fifth.png

The Output of Data Deletion Application looks like this:

EDM Sixth.png

The Output of Data Deleted Application looks like this:

EDM Seventh.png

I hope this article is useful for your. I look forward for your comments and feedback. Thanks Vijay Prativadi... 


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