Let’s now write a (very) simple page that uses our Northwind
model classes to display some data from our database.
We’ll begin by adding a new page to our ASP.NET
project. Right-click on the web project and choose Add->New Item, then
select the “Web Form using Master Page” item template. We’ll name the
page “Products.aspx” and have it use the “Site.master” master-page that is
included by default with the ASP.NET Web Project starter template.
We’ll add a <asp:GridView> control to our new
Products.aspx page. We’ll configure it to show just the name and price of
our Products:
Within our code-behind file we can then write the following
LINQ query against our model classes to retrieve all active products from our
database, and bind them to the above GridView control:
And now when we run the project and navigate to the
Products.aspx page we’ll get a listing of our Products like so:
We now have a simple application that uses EF “code first”
against an existing database.