Export (0) Print
Expand All
20 out of 35 rated this helpful - Rate this topic

Walkthrough: Creating a New Dynamic Data Web Site Using Scaffolding

This walkthrough shows you how to create a basic Web site that uses ASP.NET Dynamic Data. Dynamic Data enables you to create a data-driven Web site with little or no coding.

An important feature of Dynamic Data is the scaffolding framework. When scaffolding is enabled in a Dynamic Data Web site, ASP.NET analyzes the application's data model and generates Web pages dynamically based on the data in the data model. These auto-generated Web pages provide the capability to display, insert, delete, and edit data for each table.

A Visual Studio project with source code is available to accompany this topic: Basic Dynamic Data Web Site with Scaffolding.

See a video that shows this feature: Watch.

In order to complete this walkthrough, you will need:

You can create Dynamic Data Web sites in Visual Studio by using ASP.NET Dynamic Data templates. 

Before you proceed, you must decide if you want to create a Web site project or a Web application project. For more information, see Web Application Projects versus Web Site Projects. Follow the steps in the appropriate procedure. Most of the steps you follow in order to create a Dynamic Data Web application that uses scaffolding are the same for both Web site projects and Web application projects. However, a few steps differ depending on which type of project you are using.

To create a Dynamic Data Web site project

  1. Start Visual Studio or Visual Web Developer.

  2. In the File menu, click New Web Site.

    The New Web Site dialog box is displayed.

  3. Under Installed Templates, in the left pane, select Visual Basic or Visual C#.

  4. In the center pane, select one of the following:

    • To use the LINQ to SQL model, select ASP.NET Dynamic Data LINQ to SQL Web Site.

    • To use the ADO.NET Entity Framework model, select ASP.NET Dynamic Data Entities Web Site.

    For more information, see ASP.NET Dynamic Data Layer Customization.

  5. In the Web Location box, select File System and then 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.

  6. Click OK.

    Visual Studio creates the Web site.

Alternatively, you can create a Web application project as shown in the following procedure.

To create a Dynamic Data Web application project

  1. Start Visual Studio or Visual Web Developer.

  2. In the File menu, click New Project.

    The New Project dialog box is displayed.

    Before you proceed to the next step, you must decide which data model to use. For more information, see ASP.NET Dynamic Data Layer Customization.

  3. Under Installed Templates, in the left pane, expand Visual Basic or Visual C# selection menu.

  4. Select Web.

  5. In the center pane, select one of the following:

    • To use the LINQ to SQL model, select ASP.Net Dynamic Data LINQ to SQL Web Application.

    • To use the ADO.NET Entity Framework model, select ASP.NET Dynamic Data Entities Web Application.

  6. In the Name box, enter the name of the application.

  7. In the Location box, select enter the name of the folder where you want to keep the pages of the Web application.

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

  8. Click OK.

    Visual Studio creates the Web site.

The next step is to add a database to the project. Later you will use the database to create a data context (classes to represent database entities) and then register the data context for use by Dynamic Data.

To add the database file to the project

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

    The Add Existing Item dialog box is displayed.

  2. Enter the location where you installed the AdventureWorksLT database file (AdventureWorksLT_Data.mdf).

    NoteNote

    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. Use the procedure that applies to the data model that you want to create.

To create the data model using LINQ to SQL

  1. If you are using a Web site project and 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. Right-click the App_Code folder and then click Add New Item.

  2. If you are using a Web application project, in Solution Explorer, right-click the project, click Add, and thenclick New Item.

  3. Under Installed Templates, in the left pane, perform one of the following:

    • If you are using a Web site project, select Visual Basic or Visual C#.

    • If you are using a Web application project, expand the Visual Basic or Visual C# menu and then select Data.

  4. In the center pane, click LINQ to SQL Classes.

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

    For example, enter the name AdventureWorksLT.dbml.

  6. Click Add.

    The Object Relational Designer is displayed.

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

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

    NoteNote

    If the database file node does not exist, in Solution Explorer, double-click the database file. This will create a connection to the database and the file node will be displayed in Server Explorer.

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

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

  10. Save the AdventureWorksLT.dbml file.

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

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

  12. Open the Web.config file.

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

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

To create the data model using Entity Framework

  1. If you are using a Web site project and 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. Right-click the App_Code folder and then click Add New Item.

  2. If you are using a Web application project, in Solution Explorer, right-click the project, click Add, and then click New Item.

  3. Under Installed Templates, in the left pane, perform one of the following:

    • If you are using a Web site project, select Visual Basic or Visual C#.

    • If you are using a Web application project, expand the Visual Basic or Visual C# menu and then select Data.

  4. In the center pane, click ADO.NET Entity Data Model.

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

    For example, enter the name AdventureWorksLT.edmx.

  6. Click Add.

    The Entity Data Model Wizard window is displayed.

  7. Select Generate from database, and then click Next.

    The Entity Data Model Wizard is displayed.

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

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

  10. Click Next.

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

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

  12. Click Finish.

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

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

    Notice that the .edmx file contains the AdventureWorksLT_DataEntities class that represents the database. It also contains entity classes that represent database tables.

  14. Open the Web.config file.

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

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

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

To register the data context

  1. Open the Global.asax file.

  2. Uncomment the line that contains the DefaultModel.RegisterContext method.

  3. Set the appropriate context type and the variable ScaffoldAllTables to true If you are using the LINQ-to-SQL model, use the following code:

    Defaultmodel.RegisterContext(GetType(AdventureWorksLTDataContext), _
        New ContextConfiguration() With {.ScaffoldAllTables = True})
    

    DefaultModel.RegisterContext(typeof(AdventureWorksLTDataContext), 
        new ContextConfiguration() { ScaffoldAllTables = true });
    

    If you are using the Entity Framework model, use the following code:

    Defaultmodel.RegisterContext(GetType( _   
        AdventureWorksLT_DataEntities), _
        New ContextConfiguration() With {.ScaffoldAllTables = True})
    

    DefaultModel.RegisterContext(typeof(AdventureWorksLT_DataEntities), 
        new ContextConfiguration() { ScaffoldAllTables = true });
    

    This registers the data context for use by Dynamic Data and enables scaffolding for the data model.

    Security noteSecurity Note

    Enabling scaffolding (setting the variable ScaffoldAllTables to true) 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.

  4. Save and close the Global.asax file.

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

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.