ASP.NET Web Forms Model Binding with Entity Framework: Simplified Data Access
Simplify data access in your ASP.NET Web Forms applications using model binding with Entity Framework. This tutorial demonstrates how to work with data using .NET objects instead of directly interacting with data source objects, resulting in cleaner, more maintainable code.
Model Binding in ASP.NET Web Forms with Entity Framework
Introduction to Model Binding in Web Forms
Model binding simplifies data access in ASP.NET Web Forms. Instead of directly working with data source objects (like ObjectDataSource
or SqlDataSource
), model binding allows you to work with data using .NET objects. This makes data interaction more intuitive and easier to manage, especially when working with databases. This tutorial demonstrates model binding using Entity Framework (EF) and a GridView to display data.
Setting up the Project
- Create a new ASP.NET Web Application project in Visual Studio, selecting the Web Forms template and configuring authentication for individual user accounts.
- Create a master page to be used for displaying data.
- Create a data model (
StudentModels.cs
) and database using Code First Migrations. This model will define the structure for your database tables. (The code for StudentModels.cs is given below.)
StudentModels.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace ModelBindingDemo.Models
{
public class SchoolContextDemo : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
}
// ... (rest of the model classes: Student, Enrollment, Course, AcademicYear enum) ...
}
(The complete model classes—Student
, Enrollment
, Course
, and the AcademicYear
enum—would be included in the HTML.)
Setting up the Database
- Open the Package Manager Console (View -> Other Windows -> Package Manager Console).
- Enable migrations:
enable-migrations -Force -ContextTypeName ModelBindingDemo.Models.SchoolContextDemo
- Add a migration:
add-migration initial
- Update the database:
update-database
(Screenshots illustrating the steps to enable migrations and create and update the database would be included here.)
The Configuration.cs
file is automatically generated. Add seed data to pre-populate the database tables.
Configuration.cs
// ... (The code to add seed data to the database would be included here.) ...
Displaying Data using a GridView
Create a web form (Student.aspx
) and use a GridView control to display data from the database. Model binding simplifies data access; you don't need to manually handle database interactions in the code-behind.
Student.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Students.aspx.cs" Inherits="ModelBindingDemo.Students" %>
<asp:GridView ID="studentsGrid" runat="server" AutoGenerateColumns="false"
DataSourceID="studentsSource" >
<Columns>
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="Year" HeaderText="Year" />
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="studentsSource" runat="server"
ContextTypeName="ModelBindingDemo.Models.SchoolContextDemo"
EntitySetName="Students" />
Student.aspx.cs
using System.Linq;
using ModelBindingDemo.Models;
namespace ModelBindingDemo
{
public partial class Students : System.Web.UI.Page
{
public IQueryable<Student> studentsGrid_GetData()
{
SchoolContextDemo db = new SchoolContextDemo();
var query = db.Students.Include(s => s.Enrollments.Select(e => e.Course));
return query;
}
}
}
Output
(A screenshot showing the GridView displaying the student data would be included here.)