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.
To create a Dynamic Data Web application project
1. Start Visual Studio or Visual Web Developer.
2. In the File menu, click NewProject.
The New Project dialogue box
is displayed.
3. Under Installed Templates, in the
left pane, expand Visual C# selection menu.
4. Select Web.
5. In the centre pane,
o 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.
To create the data model using
Entity Framework
1.
We are using Web
application project so, in Solution Explorer, right-click the
project, click Add, and then click New Item.
2.
Under Installed
Templates, in the left pane, perform the following:
§ For Web application project, expand
the Visual C# menu and then select Data.
3.
In the center pane,
click ADO.NET Entity Data Model.
4.
In the Name box,
enter a name for the database model.
For example, enter the name ExpenseEntry.edmx.
5.
Click Add.
The Entity Data Model Wizard window
is displayed.
6.
Select Generate
from database, and then click Next.
The Entity Data Model Wizard is
displayed.
7.
Under Which
data connection should your application use to connect to the database?,
select ExpenseEntry_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 ExpenseEntry .designer.cs file
that is located under the .edmx file node.
Notice that the .edmx file contains the ExpenseEntry_DataEntities class
that represents the database. It also contains entity classes that represent
database tables.
13.
Open the Web.config
file.
Notice that the connectionStrings element
contains the connection string to the ExpenseEntry database.
14.
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
- 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 Entity Framework model, use the following code:
- C# Code (Uncomment and change ScaffoldAllTables False to true)
DefaultModel.RegisterContext(typeof(AdventureWorksLT_DataEntities),
new ContextConfiguration() {
ScaffoldAllTables = true });
|
7. 7. Save and close the
Global.asax file.
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
- 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.
- 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.
- Click the Delete button to delete a record from the table.
- Click the page numbers to navigate through the records.
- Click the Edit button to modify a record in the table.
- Change the values and then click Update, or click Cancel to cancel the edit operation.
- 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.
- Provide the new record information and then click Insert, or click Cancel to cancel the insert operation.
- When you have finished, close the browser.
0 comments:
Post a Comment