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.
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.
Then the Entity Data Model Wizard will open up where you need to select Generate from database option.
Now the wizard will ask you to connect and configure the connection string to the database.
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.
The above was the last step and you should now have the Entity Data Model ready with the Customers Table of the Northwind Database.
Class to serve 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.
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.
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
HTML Markup
The following HTML Markup consists of a GridView that is connected with the ObjectDataSource.
The GridView has been specified with the Properties and Methods of the CustomerDS class.
1. TypeName – CustomerDS
2. SelectMethod – GetCustomers
3. SelectCountMethod – GetCustomersCount
4. MaximumRowsParameterName - maxRows
5. StartRowIndexParameterName - startIndex
<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>
Screenshot
Demo
Downloads