In this article I will explain with an example, how to use EntityFramework in ASP.Net Web Forms.
 
 
 

Database

Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 

Configuring and connecting Entity Framework to database

1. Inside the Solution Explorer, right click on Project and then click on Add and then click on Add New Item.
Using Entity Framework in ASP.Net Web Forms
 
2. Then, inside the Add New Item window, select ADO.NET Entity Data Model option.
Using Entity Framework in ASP.Net Web Forms
 
3. As soon as you add the Entity Data Model to your project you will be prompted with the following dialog and you need to click YES button.
Using Entity Framework in ASP.Net Web Forms
 
4. Then, the Entity Data Model Wizard will open up where you need to select EF Designer from database.
Using Entity Framework in ASP.Net Web Forms
 
5. After that, click on New Connection Button.
Using Entity Framework in ASP.Net Web Forms
 
6. Now the wizard will ask you to connect and configure the connection string to the database.
Using Entity Framework in ASP.Net Web Forms
 
After connecting to the database, click on Test Connection and if connection was successful it will display the Success message.
Using Entity Framework in ASP.Net Web Forms
 
7. Then, click on Next Button to move on to the Next step.
Using Entity Framework in ASP.Net Web Forms
 
8. Now, you need to select the Table you need to connect and work with Entity Framework and click on Finish Button.
Using Entity Framework in ASP.Net Web Forms
 
Finally, the Entity Data Model ready with the Customers Table of the Northwind database.
Using Entity Framework in ASP.Net Web Forms
 
 

HTML Markup

The HTML Markup consists of following control:
GridView – For displaying data.
<h4>Customers</h4>
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerID" HeaderText="Customer Id" />
        <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
 
 

Binding GridView with Entity Framework using C# and VB.Net

Inside the Page Load event handler, the Top 10 records from the Customers table are fetched using Entity Framework
and assigned to the DataSource property of the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        using (NORTHWINDEntities entities = new NORTHWINDEntities())
        {
            gvCustomers.DataSource = (from customer in entities.Customers.Take(10)
                                      select customer).ToList();
            gvCustomers.DataBind();
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBackThen
        Using entities As NORTHWINDEntities = New NORTHWINDEntities()
            gvCustomers.DataSource = (From customer In entities.Customers.Take(10) Select customer).ToList()
            gvCustomers.DataBind()
        End Using
    End If
End Sub
 
 

Screenshot

Using Entity Framework in ASP.Net Web Forms
 
 

Downloads