The official source of information on Managed Providers, DataSet & Entity Framework from Microsoft
Last month we announced the beta release of Service Pack 1 for Visual Studio 2008. This release includes the Entity Framework, including a wizard that can be used to generate a model and a graphical model designer. It also includes the EntityDataSourceControl, which lets you bind ASP.NET data bound controls to data from a model.
This tutorial grew out of a project I created during a recent bug-bash of the Entity Framework and the modeling tools. This posting is the first of several in which I will show you how use the Entity Data Model wizard to add an AdventureWorks-based model to a simple Web project. It also shows how to bind data to controls in the web app, both through the EntityDataSource, and by executing queries directly against the entity data model.
These are the steps used to create a simple web project which we will use throughout the tutorial. The steps are written to create a C# based Web project as it’s the .NET language with which I’m most comfortable.
We won’t use the actual designer for this tutorial, but you can double-click the AdventureWorksModel.edmx file in Solution Explorer to take a look at the model that was generated by the wizard.
This page allows the user to view details about all the products AdventureWorks sells. We will add four data-bound controls and an EntityDataSource control for each as follows:
Based on the category selected in the Category drop down list, select the ProductSubcategory entity’s Name and ProductSubcategoryID properties.
Display all the products for the subcategory selected in the Subcategories grid view.
DetailsView named dvwProduct Selected fields are Name, Color, and Price
At this point you can run the Web project to verify that the control is correctly picking up the product categories from the AdventureWorks database:
This control is hooked to a grid view, and is used to display a product subcategory based on the selection in the ddCategory drop down list.
<asp:GridView ID="gvwSubcategories" runat="server" AutoGenerateColumns="False" DataSourceID="dsSubCategory" DataKeyNames="ProductSubcategoryID">
At this point you can run the Web site to verify that the grid view displays the subcategories based on the selected category:
This control is hooked to a grid view, and is used to display all products for a given subcategory, based on the selection in the Subcategories grid view
The finished markup code should look something like:
<asp:EntityDataSource ID="dsProduct"runat="server" ConnectionString="name=AdventureWorksModelConnection" DefaultContainerName="AdventureWorksModelConnection"EntitySetName="Product"Select="it.[ProductID], it.[Name]"OrderBy="it.[Name]"Where="it.ProductSubcategory.ProductSubcategoryID =@ProductSubcategoryID"> <WhereParameters> <asp:ControlParameter ControlID="gvwSubcategories"Name="ProductSubcategoryID"PropertyName="SelectedValue"Type="Int32"/> </WhereParameters></asp:EntityDataSource>
<asp:GridView ID="gvwProduct" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="dsProduct" DataKeyNames="ProductID">
At this point you can run the Web site to verify that the grid view displays the products based on the selected subcategory:
This control is hooked to a details view, and is used to display information about the product selected in the Products grid view.
<asp:EntityDataSource ID="dsProductDetails" runat="server" ConnectionString="name=AdventureWorksModelConnection" DefaultContainerName="AdventureWorksModelConnection" EntitySetName="Product" Select="it.[ProductID], it.[Name], it.[Color], it.[ListPrice]" Where="it.ProductID = @ProductID"> <WhereParameters> <asp:ControlParameter ControlID="gvwProduct" Name="ProductID" PropertyName="SelectedValue" Type="Int32" /> </WhereParameters> </asp:EntityDataSource>
<asp:TemplateField> <ItemTemplate> <a href='ProductDetails.aspx?ProductID=<%# Eval("ProductID") %>'> [More Details...]</a> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <a href='ProductReview.aspx?ProductID=<%# Eval("ProductID") %>&ProductName=<%# Eval("Name") %>'> [Product Reviews...]</a> </ItemTemplate> </asp:TemplateField>
At this point you can run the Web site to verify that the details view displays the Name, Color, and Price for the correct product:
That’s it for the first post in this tutorial. In later postings, I’ll show how to implement the code for the ProductDetails and ProductReview pages. I’ll also show how to read the images for the selected products using Entity SQL queries executed against the model.
Additional Links:
Guy Burstein has created a screencast about the EntityDataSource control on Channel 9.
Eric DettingerSoftware Design Engineer in Test, ADO.NET Entity Framework