Visual Studio 2008 SP1: EntityDataSource for ASP.Net (转载)

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.

<body>

    <form id="form1" runat="server">

    <h1>

        Using the EntityDataSource</h1>

    <asp:GridView runat="server" ID="GridView" />

    </form>

</body>

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.

Visual Studio 2008 SP1 Beta EntityDataSource

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.

Visual Studio 2008 SP1 Beta EntityDataSource

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...>

Visual Studio 2008 SP1 Beta Entity DataSource

This will start the Data Source Configuration Wizard. Select the Entity Data Source and provide a meaningful name.

Visual Studio 2008 SP1 Beta EntityDataSource

Select the name of the connection to use and the name of the container.

Visual Studio 2008 SP1 Beta EntityDataSource

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.

Visual Studio 2008 SP1 Beta EntityDataSource

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.

Visual Studio 2008 SP1 Beta EntityDataSource

And in the source view it looks like:

<asp:GridView runat="server" ID="GridView" " DataKeyNames="CustomerID"

    DataSourceID="EntityDataSource" >

    <Columns>

        ...

    </Columns>

</asp:GridView>

<asp:EntityDataSource ID="EntityDataSource" runat="server"

    ConnectionString="name=BankEntities" ContextTypeName=""

    DefaultContainerName="BankEntities" EntitySetName="Customers">

</asp:EntityDataSource>

6. If we now run this application, we can see the details in the grid.

Visual Studio 2008 SP1 Beta EntityDataSource

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.

Enjoy!

posted @ 2008-08-22 10:33  InFuture  阅读(1517)  评论(0编辑  收藏  举报