Entity Framework Database First With ASP.NET WebForm

In this article you will get answers to the following questions.

  • What is Database First?
  • How do we  implement Entity Framework Database First in WebForm?

Entity Framework is ORM (Object Relation Mapping) tool which is used to get connected with the database. Entity Framework is a part of ADO.NET under .NET Framework. There are three approaches to get connected with database.

  1. Database First
  2. Model First
  3. Code First

What is Database First?

In this method, model or entity is generated as per database. First, we have to design the database and then, the rest of the things are created. When we go for a Database First approach, we’re fully dependent on what gets generated for our models in our application. Occasionally, the naming convention is undesirable and not as we want. Sometimes, the relationships and associations aren't quite what we want. Code First is better than Database First in this way.

This method is good where project gets developed on the basis of database and the existing application is developed in Windows Form.

My database name is MbkTest. It has one table called tblFriends.

tblFriends table structure 

  1. USE [MBKTest] GO  
  2. /****** Object: Table [dbo].[tblFriends] Script Date: 10-Jan-17 5:49:25 PM ******/  
  3. SET  
  4. ANSI_NULLS ON GO  
  5. SET  
  6. QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[tblFriends](  
  7. [FriendID] [int] IDENTITY(1, 1) NOT NULL,  
  8. [FriendName] [nvarchar](50) NULL,  
  9. [FriendPlace] [nvarchar](50) NULL,  
  10. [FriendMobile] [nvarchar](10) NULL,  
  11. [FriendEmailID] [nvarchar](200) NULL,  
  12. CONSTRAINT [PK_tblFriends] PRIMARY KEY CLUSTERED ([FriendID] ASCWITH (  
  13. PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,  
  14. IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,  
  15. ALLOW_PAGE_LOCKS = ON  
  16. ON [PRIMARY]  
  17. ON [PRIMARY] GO   

Sample records of table tblFriends.


How to implement Entity Framework Database First in Web Form



Right click on project and select Add >> Add New Item >> WebForm.


Add a new Web Form named “Default.aspx”.


To implement the database first approach, we should have ADO.NET Entity Data Model. 

Right click on the project name and select Add-->Add New Item >> ADO.NET Entity Data Model.


Add a new ADO.NET Entity Data Model named “MbkTestModel.edmx”.


As you click on "Add" button, the following dialog box will appear on the screen. This dialog box is asking you to put ADO.NET Entity data model in APP_CODE folder. Please select "Yes".


As you click on Yes button, the Entity Data Model Wizard dialog box will appear on the screen, asking you to select any one out of two options.

Following are the two options.

  1. Generate from database To generate a model from existing database.
  2. Empty Model To generate a database from model.

We will select the first option i.e. "Generate from database". Because we are using Database First, so ADO.NET Entity data model gets generated from the existing database.


As you click on "Next" button, the following screen will appear.


Now, click on "New Connection" button to specify your connection. As you click, the following screen appears.


As you click on "OK" button, the following screen will appear.


I have given connection string name as “MBKTestDbContext”. As you click on "Next" button, the following screen will appear.


As you click on "Finish" button, this screen will appear.




You ADO.NET Entity model EDMX file will display look like this.


Now, switch back to default.aspx page, double click on it, and drag and drop the GridView control on it.


After dragging and dropping the GridView.




Default.aspx Code 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.     <!DOCTYPE html>  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div>  
  12.                 <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">  
  13.                     <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />  
  14.                     <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />  
  15.                     <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />  
  16.                     <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />  
  17.                     <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />  
  18.                     <SortedAscendingCellStyle BackColor="#FFF1D4" />  
  19.                     <SortedAscendingHeaderStyle BackColor="#B95C30" />  
  20.                     <SortedDescendingCellStyle BackColor="#F1E5CE" />  
  21.                     <SortedDescendingHeaderStyle BackColor="#93451F" /> </asp:GridView>  
  22.             </div>  
  23.         </form>  
  24.     </body>  
  25.   
  26.     </html>   

Default.aspx.cs Code 

  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. public partial class _Default: System.Web.UI.Page {  
  8.     protected void Page_Load(object sender, EventArgs e) {  
  9.         //Created a instance of MBKTestDbContext as _mbkTestDbContext.  
  10.         MBKTestDbContext _mbkTestDbContext = new MBKTestDbContext();  
  11.         //Filling & Binding the gridview.  
  12.         GridView1.DataSource = _mbkTestDbContext.tblFriends.ToList();  
  13.         GridView1.DataBind();  
  14.         //Very simple and straight forward code.  
  15.     }  
  16. }   

Output


Links

You can find the details about Code First  here. In the upcoming articles, we will discuss implementing Entity Framework Database First with ASP.NET MVC.