Visual Studio 2008 SP1: EntityDataSource for ASP.Net
EntityDataSource is a new DataSource control for ASP.Net (just like ObjectDataSource and SQLDataSource and more recent – LinqDataSource) which makes declaratively binding ASP.NET UI controls to Entity Data Models very easy.
In this post I will build a simple web application that uses EntityDataSource in order to demo how it is being used.
1. Create a simple Web Application, and create an initial GridView on it.
<form id="form1" runat="server">
Using the EntityDataSource</h1>
<asp:GridView runat="server" ID="GridView" />
2. Create a new ADO.Net Entity Data Model. Add a new ADO.Net Entity Data Model item to the project, and generate a model from an existing database. For this sample I am using the Bank DB Schema, but you can use any other schema as well.
3. If the EntityDataSource is not shown in the Toolbox under the Data category – Add it. Right click the toolbox and select the Choose Items… option.
In the Choose Toolbox Item dialog, set the filter to Entity and check the EntityDataSource component.
4. Add a reference to System.Web.Entity.dll. The EntityDataSource component is part of a new assembly in SP1 called System.Web.Entity.dll, and its designer support components can be found in System.Web.Entity.Design.dll.
5. Bind the grid to the EntityDataSource. Switch to design view and select the gridview to display the smart tag. In the Choose Data Source drop down select the option <new data source…>
This will start the Data Source Configuration Wizard. Select the Entity Data Source and provide a meaningful name.
Select the name of the connection to use and the name of the container.
Select the name of the EntitySet you want to display its entities and the columns you would like to display. If the EntitySet contains a hierarchy of entity types, you can filter the type you want.
When we click the "Finish" button, VS 2008 will declare a <asp:EntityDataSource> within the .aspx page, and update the <asp:gridview> to point to it.
And in the source view it looks like:
<asp:GridView runat="server" ID="GridView" " DataKeyNames="CustomerID"
<asp:EntityDataSource ID="EntityDataSource" runat="server"
6. If we now run this application, we can see the details in the grid.
In this post I had a step by step guide on how to use the EntityDataSource in a very basic way. In the next post I'll show how to filter entities using a where clause.