Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 Walkthrough: Creating a New ASP.NET...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Walkthrough: Creating a New ASP.NET Dynamic Data Web Site Using Scaffolding

Updated: July 2008

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.

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

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.

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.

    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.

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:

    Visual Basic
    model.RegisterContext(GetType(AdventureWorksDataContext), _
        New ContextConfiguration() With {.ScaffoldAllTables = True})
    

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

    Caution noteCaution:

    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:

    Visual Basic
    model.RegisterContext(GetType(AdventureWorksLT_DataModel.AdventureWorksLT_DataEntities), _
        New ContextConfiguration() With {.ScaffoldAllTables = True})
    

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

    Caution noteCaution:

    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.

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.

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.

Date

History

Reason

July 2008

Added topic for new feature.

SP1 feature change.

Community Content   What is Community Content?
Add new content RSS  Annotations
Some steps incorrect (and therefore confusing)      cori schlegel ... Thomas Lee   |   Edit   |   Show History
When trying to work through this walk-through, I discovered that the App_Code asp.net folder is not one of the options for "Add ASP.Net Folder". I attempted to create the folder to hold the dbml files manually, and add my database model files to the new folder, but when doing so, adding the datacontext to Global.asax.cs failed becuase Global.asax couldnt find the referenced data context.

I look at the DataContext designer.cs file and discovered that it was creating the DataContext in the MyProject.App_Code namespace, and not the MyProject namespace. I tried adding a using statement for that sub-namespace, but te compiler couldn't recognize that namespace.

What ended up working (at least to get the site to compile) was to delete the manually-created App_Code folder and add the dbml file to the root of the site. Then everything worked as the walk-through seemed to indicate.

Using VS 2008/>net 3.5 SP 1


Add App_Code not an option      tex222   |   Edit   |   Show History
I had a similar problem in that I wasn't allowed to add the App_Code folder. MOved the dbml to the root and changed the compile setting for the .vb file to Compile. Then it was able to be compiled. Still didn't work though. What a letdown...
Flag as ContentBug
Add App_Code not an option (Part 2) - Here's the error      tex222   |   Edit   |   Show History

There are no accessible tables. Make sure that at least one data model is registered in Global.asax and scaffolding is enabled or implement custom pages.

Tags What's this?: Add a tag
Flag as ContentBug
Dynamic Data = The Promised Land      remesq   |   Edit   |   Show History
I'm a lay .net person. I have been using it for my business website and now am trying to create a small web-based application for my clients. I stepped away from ASP.net for a few months. When I return I (que the choir) find out about Dynamic Data.

Don't know about you, but IMHO this is the best advance in ASP.net. I look forward to wrapping my head around this and using it.

This was a simple project to set up. Follow the Dynamic Data videos on www.asp.net because they are very helpful as well.
Tags What's this?: Add a tag
Flag as ContentBug
Add App_Code not an option ~ What worked for me      Chris Strolia-Davis ... Thomas Lee   |   Edit   |   Show History
Hey all, I was dealing with this same issue and as it turns out, I was being too smart for my own good.

When I created the new project, I had not created the New Website, option, I had simply said new project. The same project type exists there and it even compiles and runs a web page, so it hadn't occurred to me that there was any issue.

Of course, I wasn't seeing the app_code folder either. Well, at some point, a little light bulb went off and I decided to start the project specifically as a web site. Now I see the app_code folder option and I assume it will work, although I haven't actually tried it out yet.
No App_Code folder and No Accessible Tables Exception      Aaron Hoffman ... Thomas Lee   |   Edit   |   Show History

No App_Code folder and No Accessible Tables Exception

The reason you are not seeing the App_Code folder under the Add >> Add ASP.NET Folder... option is probably because it is a Web Application project and not a WebSite Project. If you are not seeing the App_Code folder as an option, you can just add the LINQ to SQL Classes file (.dbml) to the root of the project.

You are probably seeing the No Accessible Tables Exception because in the RegisterRoutes method the “ScaffoldAllTables” propery of the ContextConfiguration is still set to FALSE. Make sure it is set to TRUE.

Flag as ContentBug
This doesn't even work for the MemberShip database from Microsoft.      Bernie Quick   |   Edit   |   Show History

I created a site no problem. I created one to manage the Membership Database (Yes I know there is a built in tool for this, but we need one to manage multiple applications, and right now, the tool only manages one)

So, I got this all hooked up in about 20 minutes. Then tried to use the site.

Problem Number 1: In the aspnet_Applications table, there is a guid() field which has a default value set to newid(). Yet on the screen you are asking the use for the guid. You should be able to see that default value and tell the user interface not to generate a control for the ApplicationID. That will be handled by the database on insert.

Problem Number 2: Try and enter a duplicate Application Name. You get a javascript error: (it's so bad, I can't even copy the error..have to type it here:
Char:21

Error: Sys.WebForms.PageRequestManagerServerErrorException: An error occurred while updating the entities. See hte InnerException for details"

And I cannot see the inner exception (very annoying)

Problem Number 3: I try to view users and get:

DataBinding: 'System.Web.UI.WebControls.EntityDataSourceWrapper' does not contain a property with the name 'aspnet_Membership'.

Problem Number 4: asp_net_Profile table insert is asking for a UserID...when at the bottom of the form you have a dropdown for the Users...I don't get that...

Maybe this thing works for really really simple database, but it doesn't even work for the Membership Database.

re: ... does not contain a property....exception      Daniel Martini ... Thomas Lee   |   Edit   |   Show History

This is a known bug with a fix available. Start here: http://support.microsoft.com/kb/959209/. My installation was fixed automatically with Windows Update and can also be patched manually from the downloads here: http://www.microsoft.com/downloads/details.aspx?FamilyID=98E83614-C30A-4B75-9E05-0A9C3FBDD20D&displaylang=en&displaylang=en .

As for the rest of it, you are going to have to dig deeper and learn a bit more about EF and Dynamic Data. Look specifically at DataAnnotations and at attributes that you can apply to tables and columns and other documented means of manually creating your own column sets and constraints. DD is great and can do a lot with no-code, but while amazing, it still isn't magic. You have to learn how and why it works and use what you learn to customize your projects to your needs.

Auto increment keys need manual changes      Quirijn   |   Edit   |   Show History

Great stuff. One thing that could be improved is support for auto increment keys. I needed to switch off scaffolding for all my 'id' columns in the auto-generated data model code. Otherwise these fields show up in the interface when trying to insert new records.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker