This
Post is to show 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.
Creating a Dynamic Data Web Site
1. Start Visual Studio
or Visual Web Developer.
2. In the File menu,
click NewWeb Site.
3. The New Web Site dialog
box is displayed.
4. Under Installed
Templates, in the left pane, select Visual Basic or Visual
C#.
5. In
the centre pane, select one of the following:
- To use the
LINQ to SQL model, select ASP.NET Dynamic Data LINQ to SQL Web
Site
6. 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.
7. Click OK.
Visual Studio creates the
Web site.
Adding Data to 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).
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 Post.
|
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:
o If
you are using a Web site project, select Visual
Basic or Visual C#.
o 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.
Note
|
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 Productand 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 register the data context
- Open the Global.asax file.
- Uncomment the line that contains the DefaultModel.RegisterContext method.Set the appropriate context type and the variable ScaffoldAllTables to true.For the LINQ-to-SQL model, use the following code:
DefaultModel.RegisterContext(typeof(AdventureWorksLTDataContext),
new ContextConfiguration() { ScaffoldAllTables = true });
Security
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.
|
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.
2.
The page displays a list
that contains the tables that you added to the data model.
3.
Click one of the tables.
For example, click the Products table.
4.
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.
5.
Click the Delete button to delete a record from the
table.
6.
Click the page numbers to navigate through the records.
7.
Click the Edit button to modify a record in the table.
8.
Change the values and then click Update, or
click Cancel to cancel the edit operation.
9.
At the bottom of the page, click the Insert new
item button to create a new record.
10. A page is displayed that contains data
entry fields.
11. Provide
the new record information and then click Insert, or
click Cancel to cancel the insert operation.
12. When you
have finished, close the browser.