Pages

Thursday, January 17, 2013

Creating a New Dynamic Data Web Site Using Scaffolding (Web application project-ADO.NET Entity Framework model)


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

  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 
  4.        For Entity Framework model, use the following code:
  5.        C# Code (Uncomment and change ScaffoldAllTables  False  to true)
DefaultModel.RegisterContext(typeof(AdventureWorksLT_DataEntities),
                         new ContextConfiguration() { ScaffoldAllTables = true });

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

7.             7. Save and close the Global.asax file.

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.


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.




0 comments:

Post a Comment