Articles → .NET → Basic Example Of Asp.Net Dynamic Data

Basic Example Of Asp.Net Dynamic Data






Objective Of This Tutorial





Prerequisites




  1. Visual studio 2010 is installed on your machine.
  2. Visual Studio 2010 service pack 1 is installed on your machine.




  1. How to create a project in Visual studio 2010?
  2. How to add new item in the project?
  3. Basic understanding of entity data model.
  4. What is the purpose of global.asax file?
  5. Basic understanding of SQL.

Steps Involved




  1. Create a new project of type ‘ASP.NET Dynamic Entities Web Application’.
  2. Create tables at the backend
  3. Add a new entity data model
  4. Add code to the global.asax.
  5. Run the application

Step 1: Create A New Project Of Type ‘ASP.NET Dynamic Entities Web Application’




  1. Start Visual Studio.
  2. Go to File - New Project
  3. ‘New Project’ dialog box will be displayed.
  4. Select ‘Web’ under ‘Installed Templates’
  5. Select ‘ASP.NET Dynamic Data Entities Web Application’.
  6. Provide ‘Name’, ‘Location’ and ‘Solution name’ as shown below:
Picture showing selecting asp.net dynamic data project from project template
Click to Enlarge




Step 2: Create Tables At The Backend




Picture showing schema of class table
Click to Enlarge

Picture showing schema of section table
Click to Enlarge



Picture showing the data in class table
Click to Enlarge

Picture showing the data in section table
Click to Enlarge


Step3: Add A New Entity Data Model




  1. In the solution explorer, right click on project and click on Add - New item.
  2. Now select ‘ADO.NET Entity Data Model’.
  3. Give it a name which in this case is ‘Model1.edmx’ as shown in figure below:
  4. Picture showing selecting the ado.net entity data model
    Click to Enlarge

  5. Click ‘Add’ button.


  1. The first step is to choose the ‘Generate from Database’ option so that you can create .edmx file from the database that you have just created.
  2. Picture showing the selection of generate from database option
    Click to Enlarge

  3. Click ‘Next’.
  4. In the next step you have to ‘Choose Your Database Connection’.
  5. Picture showing selection of data source
    Click to Enlarge

  6. Click on ‘New Connection’ to open the ‘Connection Properties’ of your database. A Window appears as shown in figure below
  7. Picture showing selection of database
    Click to Enlarge

  8. Enter details of ‘Server name’ and ‘database name’ and click Ok. This will take us back to ‘Entity Data Model Wizard’ as shown below
  9. Picture showing the connection string of entity model
    Click to Enlarge

  10. Click next
  11. Select Tables and click Finish
Picture showing selection of class and section table for entity model
Click to Enlarge



Picture showing adding the entity model model1.edmx in solution explorer
Click to Enlarge


Step 4: Add Code To The Global.Asax




public static void RegisterRoutes(RouteCollection routes) {
	DefaultModel.RegisterContext(typeof(DynamicDataEntities), new ContextConfiguration() {
		ScaffoldAllTables = true
	});
	//                    IMPORTANT: DATA MODEL REGISTRATION 
	// Uncomment this line to register an ADO.NET Entity Framework model for ASP.NET Dynamic Data.
	// Set ScaffoldAllTables = true only if you are sure that you want all tables in the
	// data model to support a scaffold (i.e. templates) view. To control scaffolding for
	// individual tables, create a partial class for the table and apply the
	// [ScaffoldTable(true)] attribute to the partial class.
	// Note: Make sure that you change "YourDataContextType" to the name of the data context
	// class in your application.
	//DefaultModel.RegisterContext(typeof(YourDataContextType), new ContextConfiguration() { ScaffoldAllTables = false });

	// The following statement supports separate-page mode, where the List, Detail, Insert, and 
	// Update tasks are performed by using separate pages. To enable this mode, uncomment the following 
	// route definition, and comment out the route definitions in the combined-page mode section that follows.
	routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
		Constraints = new RouteValueDictionary(new {
			action = "List|Details|Edit|Insert"
		}),
		Model = DefaultModel
	});

	// The following statements support combined-page mode, where the List, Detail, Insert, and
	// Update tasks are performed by using the same page. To enable this mode, uncomment the
	// following routes and comment out the route definition in the separate-page mode section above.
	//routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
	//    Action = PageAction.List,
	//    ViewName = "ListDetails",
	//    Model = DefaultModel
	//});

	//routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
	//    Action = PageAction.Details,
	//    ViewName = "ListDetails",
	//    Model = DefaultModel
	//});
}



Step 5: Run The Application




Picture showing the output with class and section table names
Click to Enlarge



Picture showing the output of class table
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, March 13, 2014

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250