Dela via


Walkthrough: Creating a New ASP.NET Dynamic Data Web Site Using Scaffolding

This walkthrough shows you how to create a basic Web application that uses ASP.NET Dynamic Data. Dynamic Data enables you to create a data-driven Web site with minimal or no additional code. An important feature of Dynamic Data is the scaffolding mechanism. When the scaffolding mechanism is enabled in a Dynamic Data Web site, ASP.NET analyzes your data model and generate Web pages dynamically for each table. These auto-generated Web pages provide display, insert, delete, and edit capabilities for each table.

In this walkthrough you will build an application that displays pages of data from the AdventureWorks sample database.

See a video that shows this feature: Watch.

Prerequisites

In order to complete this walkthrough, you will need:

  • Microsoft Visual Studio 2008 Service Pack 1 or Visual Web Developer 2008 Express Edition Service Pack 1.

  • Either the AdventureWorks or the AdventureWorksLT sample database. For information about how to download and install the SQL Server sample database, see Microsoft SQL Server Product Samples: Database on the CodePlex site. Make sure that you install the correct version of the sample database for the version of SQL Server that you are running (Microsoft SQL Server 2005 or Microsoft SQL Server 2008). 

Creating a Dynamic Data Web Site

You can create Dynamic Data Web sites in Visual Studio by using a Web site template.

To create a Dynamic Data Web site

  1. Start Visual Studio or Visual Web Developer.

  2. In the File menu, click New Web Site. Or if you do not have this option, click New, and then click Web Site.

    The New Web Site dialog box is displayed.

  3. Under Visual Studio installed templates, select Dynamic Data Web Site (to use LINQ to SQL) or Dynamic Data Entities Web Site (to use the ADO.NET Entity Framework).

  4. In the first Location box, select File System, and in the second box, enter the name of the folder where you want to keep the pages of the Web site.

    For example, enter the folder name C:\WebSites\DynamicData.

  5. In the Language list, click the programming language that you prefer to work in.

  6. Click OK.

  7. Visual Studio creates the folder and the structure for the Web site.

Adding Data to the Web Site

The next step is to add a database to the project. To do so, you create classes to represent database entities by using a tool provided by Visual Studio, and then register the data context for use by Dynamic Data. You have the following choices to create the database model based on the template you selected:

  • If you created the Web site using the Dynamic Data Web Site template, you must create the database model using LINQ to SQL.

  • If you created the Web site using the Dynamic Data Entities Web Site template, you must create the database model using the Entity Framework.

For more information about selecting the data model, see ASP.NET Dynamic Data Guidelines.

To add the database file to the project

  1. If the Web site does not have an App_Data folder, in Solution Explorer, right-click the project, click Add ASP.NET Folder, and then click App_Data.

  2. In Solution Explorer, right-click the App_Data folder, and then click Add Existing Item.

    The Add Existing Item dialog box is displayed.

  3. Enter the location where you installed the AdventureWorks database file (AdventureWorks_Data.mdf).

    By default, the .mdf file is installed in the path C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.mdf.

    Note

    This procedure will create a copy of the database file in the project. If it is impractical to make a copy of the database, you can connect to it by using an alternative method, such as attaching the database file directly. However, the procedure for doing this is not covered in this walkthrough.

The next step is to create the data model. The procedure differs slightly depending on whether you want to use LINQ to SQL or the ADO.NET Entity Framework to create the data model.

To create the data model using LINQ to SQL

  1. If the Web site does not already have an App_Code folder, in Solution Explorer, right-click the project, click Add ASP.NET Folder, and then click App_Code.

  2. Right-click the App_Code folder and then click Add New Item.

  3. Under Visual Studio installed templates, click LINQ to SQL Classes.

  4. In the Name box, enter a name for the database model.

    For example, enter the name AdventureWorks.dbml.

  5. Click Add.

    The Object Relational Designer is displayed.

  6. In the O/R Designer, click the Server Explorer link (Database Explorer in Visual Web Developer).

  7. In Server Explorer (Database Explorer), under Data Connections, expand the database file node and then the Tables node.

  8. Drag all tables into the O/R Designer.

    Each table is represented as an entity that is named for the corresponding database table.

  9. Save the AdventureWorks.dbml file.

  10. In Solution Explorer, open the AdventureWorks.designer.cs or AdventureWorks.designer.vb file that is located under the .dbml file node.

    Notice that the .dbml file contains the AdventureWorksDataContext class that represents the database. It also contains entity classes, such as the Product and Employee classes, that represent database tables. The parameterless constructor for the AdventureWorksDataContext class reads the connection string from the Web.config file.

  11. Open the Web.config file.

    Notice that the connectionStrings element contains the connection string to the AdventureWorks database.

  12. Close the class file and the Web.config file.

To create the data model for the Entity Framework

  1. If the Web site does not already have an App_Code folder, in Solution Explorer, right-click the project, click Add ASP.NET Folder, and then click App_Code.

  2. Right-click the App_Code folder and then click Add New Item.

  3. Under Visual Studio installed templates, click ADO.NET Entity Data Model.

  4. In the Name box, enter a name for the database model.

    For example, enter the name AdventureWorks.edmx.

  5. Click Add.

    The Entity Data Model Wizard window is displayed.

  6. Click Generate from database.

    This specifies that you want to generate the model from a database.

  7. Under Which data connection should your application use to connect to the database?, select AdventureWorks_Data.mdf from the list.

  8. Make sure that the Save entity connection settings in Web.config as check box is selected. You can leave the default connection string name.

  9. Click Next.

    The wizard displays a page where you can specify what database objects you want to include in your model.

  10. Select the Tables node to select all tables from the database. You can leave the default model namespace.

  11. Click Finish.

    The ADO.NET Entity Data Model Designer is displayed. Close the designer.

  12. In Solution Explorer, open the AdventureWorks.designer.cs or AdventureWorks.designer.vb file that is located under the .edmx file node.

    Notice that the .edmx file contains the AdventureWorksDataContext class that represents the database. It also contains entity classes, such as the Product and Employee classes, that represent database tables.

  13. Open the Web.config file.

    Notice that the connectionStrings element contains the connection string to the AdventureWorks database..

  14. Close the class file and the Web.config file.

The next step is to register the data model for use by Dynamic Data.

To register the data context

  1. Open the Global.asax file.

  2. If you are using LINQ to SQL, in the RegisterRoutes method, add the following line:

    model.RegisterContext(GetType(AdventureWorksDataContext), _
        New ContextConfiguration() With {.ScaffoldAllTables = True})
    
    model.RegisterContext(typeof(AdventureWorksDataContext), 
        new ContextConfiguration() { ScaffoldAllTables = true });
    

    This registers the LINQ to SQL data context for use by Dynamic Data and enables the automatic scaffolding of the data model.

    Warning

    Enabling the scaffolding can pose a security risk because you are exposing all the tables in the data model for display and edit operations. For more information, see ASP.NET Dynamic Data Scaffolding and Page Templates Overview.

  3. If you are using the Entity Framework, in the RegisterRoutes method, add the following line:

    model.RegisterContext(GetType(AdventureWorksLT_DataModel.AdventureWorksLT_DataEntities), _
        New ContextConfiguration() With {.ScaffoldAllTables = True})
    
    model.RegisterContext(typeof(AdventureWorksLT_DataModel.AdventureWorksLT_DataEntities), 
        new ContextConfiguration() { ScaffoldAllTables = true });
    

    This registers the Entity Framework data context for use by Dynamic Data and enables the automatic scaffolding of the data model.

    Warning

    Enabling the scaffolding can pose a security risk because you are exposing all the tables in the data model for display and edit operations. For more information, see ASP.NET Dynamic Data Scaffolding and Page Templates Overview.

  4. Save and close the Global.asax file.

Testing the Dynamic Data Web Site

You can now test the Dynamic Data Web site that you just created.

To test the Web site

  1. In Solution Explorer, right-click the Default.aspx page, and then click View in Browser.

    The page displays a list that contains the tables that you added to the data model.

  2. Click one of the tables. For example, click the Products table.

    A page is displayed that contains the data from the table that you selected. Note that for tables that contain foreign-key fields, a link is provided to the details page of the referenced table. If the table is a parent table in a one-to-many relationship, a link is provided to the list page of the child table.

  3. Click the Delete button to delete a record from the table.

  4. Click the page numbers to navigate through the records.

  5. Click the Edit button to modify a record in the table.

  6. Change the values and then click Update, or click Cancel to cancel the edit operation.

  7. At the bottom of the page, click the Insert new item button to create a new record.

    A page is displayed that contains data entry fields.

  8. Provide the new record information and then click Insert, or click Cancel to cancel the insert operation.

  9. When you have finished, close the browser.

Next Steps

This walkthrough illustrates how you can create a basic Dynamic Data Web site without writing any code. From here, you can begin to explore Dynamic Data features and to extend the default behavior of Dynamic Data.

See Also

Concepts

ASP.NET Dynamic Data Overview

Other Resources

Object Relational Designer (O/R Designer)

LINQ to SQL

ADO.NET Entity Framework

Change History

Date

History

Reason

July 2008

Added topic for new feature.

SP1 feature change.