In this article I will explain with a very basic example how to configure and use Entity Framework in ASP.Net Web applications.
In this example I will bind an ASP.Net GridView using Entity Framework and also implement customized pagination using LINQ.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here
 
 
Configuring and connecting Entity Framework to database
Now I will explain the steps to configure and add Entity Framework and also how to connect it with the database.
You will need to add Entity Data Model to your project using Add New Item Dialog as shown below

ASP.Net Entity Framework GridView Paging Example
 
As soon as you add the Entity Data Model to your project you will be prompted with the following dialog. You need to click YES button.

gridview custom paging entity framework
 
Then the Entity Data Model Wizard will open up where you need to select Generate from database option

entity framework basic tutorial
 
Now the wizard will ask you to connect and configure the connection string to the database.

entity framework tutorial with example
 
You need to select the
1.     SQL Server Instance
2.     Database
And the click Test Connection to make sure all settings are correct. Finally press OK to move to the next step.
Next you will need to choose the Tables you need to connect and work with Entity Framework. Since I need to work on the Customers Table I have checked that

asp.net entity framework tutorial
 
The above was the last step and you should now have the Entity Data Model ready with the Customers Table of the Northwind Database
sample application using entity framework
 
Creating a Class to server data to the ObjectDataSource
I have created a class named as CustomerDS in the App_Code folder of the ASP.Net Website, which contains methods for returning Page Wise records of the Customers using LINQ and also to return the Count of Total Records. This two methods are required by the ObjectDataSource.
C#
using System;
using System.Web;
using System.Linq;
using NorthwindModel;
using System.Collections.Generic;
 
public class CustomerDS
{
    public List<Customer> GetCustomers(int startIndex, int maxRows)
    {
        using (NorthwindEntities entities = new NorthwindEntities())
        {
            return (from customer in entities.Customers
                    select customer)
                        .OrderBy(customer => customer.CustomerID)
                        .Skip(startIndex)
                        .Take(maxRows).ToList();
        }
    }
 
    public int GetCustomersCount()
    {
        using (NorthwindEntities entities = new NorthwindEntities())
        {
            return entities.Customers.Count();
        }
    }
}
 
VB.Net
Imports System.Linq
Imports NorthwindModel
Imports System.Collections.Generic
 
Public Class CustomerDS
    Public Function GetCustomers(startIndex As Integer, maxRows As Integer) As List(Of Customer)
        Using entities As New NorthwindEntities()
            Return (From customer In entities.Customers) _
                .OrderBy(Function(customer) customer.CustomerID) _
                .Skip(startIndex) _
                .Take(maxRows).ToList()
        End Using
    End Function
 
    Public Function GetCustomersCount() As Integer
        Using entities As New NorthwindEntities()
            Return entities.Customers.Count()
        End Using
    End Function
End Class
 
The first method GetCustomers returns Page Wise List of Customers to the ObjectDataSource using LINQ. While the second method GetCustomersCount returns the total records present in the table
You will also notice that I am passing few parameters startIndex and maxRows which are used to do custom pagination using Entity Framework and LINQ, which are also defined in the ObjectDataSource
Finally below is the GridView that is connected with the ObjectDataSource.
<asp:GridView ID="gvCustomers" CssClass="Grid" runat="server" AutoGenerateColumns="false"
    PageSize="10" AllowPaging="true" DataSourceID="dsCustomers">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
        <asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
<asp:ObjectDataSource ID="dsCustomers" runat="server" EnablePaging="true" SelectMethod="GetCustomers"
    SelectCountMethod="GetCustomersCount" TypeName="CustomerDS" MaximumRowsParameterName="maxRows"
    StartRowIndexParameterName="startIndex"></asp:ObjectDataSource>
 
Above you will notice that I specified the variable names and method names of the CustomerDS class
1.     TypeName – CustomerDS
2.     SelectMethod – GetCustomers
3.     SelectCountMethod – GetCustomersCount
4.     MaximumRowsParameterName - maxRows
5.     StartRowIndexParameterName -  startIndex
That’s all you need to do and now simply execute the project and you will see the GridView populated with records from the Customers table

gridview entity framework example
 
Demo
 
Downloads