Databases Reference
In-Depth Information
In the code-behind shown in Listing 1.2, the NorthwindEntities class is generated by
Visual Studio based on the information in the Northwind.edmx in this project. This class
encapsulates the database connection and allows you to use LINQ (language-integrated
query) to select records from the Customers table. LINQ resembles the traditional SQL
syntax to which many database developers are accustomed. It is usually a little more
compact, but more importantly, it is safer and easier to maintain. The Entity Framework
automatically generates parameterized SQL queries, eliminating SQL injection vulnerabili-
ties, and allows the compiler to check the query correctness and find typos in table and
column names that would otherwise go unnoticed until the query is executed at run time.
The resulting web page is shown in Figure 1.7.
LISTING 1.2 Entity Framework Sample (Code-Behind)
using System;
using System.Linq;
using DataModel;
namespace WebApplication.Samples.Ch01.EntityFramework
{
public partial class SamplePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (NorthwindEntities context = new NorthwindEntities())
{
var query = from c in context.Customers
select c;
this.gridView.DataSource = query.ToList();
this.gridView.DataBind();
}
}
}
}
 
Search WWH ::




Custom Search