EntityDataSource in ASP.NET

Introduction

This article demonstrates an interesting and very useful concept in Entity Framework.

Question: What is entity data source?

In simple terms "It is a data source that performs CRUD, filtering, querying and so on, all based on a data model. This reduces the development effort by performing various operations in an easy and manageable manner".

Step 1: Create a new web application

Output1.jpg

Step 2: Add a new Entity Framework model

Output2.jpg

Output3.png

Step 3: Adding a new grid view and choosing data source

Output4.png

Output5.jpg

Output6.png

Output7.png

Output8.png

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

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="EntityDataSourceApp.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.     <center>  
  10.         <div>  
  11.             <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"  
  12.                 AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan"  
  13.                 BorderWidth="1px" CellPadding="2" DataKeyNames="EmpId" DataSourceID="EntityDataSource1"  
  14.                 ForeColor="Black" GridLines="None">  
  15.                 <AlternatingRowStyle BackColor="PaleGoldenrod" />  
  16.                 <Columns>  
  17.                     <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />  
  18.                     <asp:BoundField DataField="EmpId" HeaderText="EmpId" ReadOnly="True" SortExpression="EmpId" />  
  19.                     <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />  
  20.                     <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />  
  21.                     <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />  
  22.                 </Columns>  
  23.                 <FooterStyle BackColor="Tan" />  
  24.                 <HeaderStyle BackColor="Tan" Font-Bold="True" />  
  25.                 <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />  
  26.                 <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />  
  27.                 <SortedAscendingCellStyle BackColor="#FAFAE7" />  
  28.                 <SortedAscendingHeaderStyle BackColor="#DAC09E" />  
  29.                 <SortedDescendingCellStyle BackColor="#E1DB9C" />  
  30.                 <SortedDescendingHeaderStyle BackColor="#C2A47B" />  
  31.             </asp:GridView>  
  32.             <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=CompanyEntities"  
  33.                 DefaultContainerName="CompanyEntities" EnableDelete="True" EnableFlattening="False"  
  34.                 EnableInsert="True" EnableUpdate="True" EntitySetName="tblEmployee">  
  35.             </asp:EntityDataSource>  
  36.         </div>  
  37.     </center>  
  38.     </form>  
  39. </body>  
  40. </html>
Step 5: The complete code of WebForm1.aspx.cs is as in the following:
  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;  
  7. namespace EntityDataSourceApp  
  8. {  
  9.     public partial class WebForm1 : System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.         }  
  14.     }  
  15. }
Step 6: The output of the application is as in the following:

Output9.png

I hope this article was useful for you.

 


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