Articles → .NET → Basic Example Of Asp.Net Dynamic Data
Basic Example Of Asp.Net Dynamic Data
Objective Of This Tutorial
Prerequisites
- Visual studio 2010 is installed on your machine.
- Visual Studio 2010 service pack 1 is installed on your machine.
- How to create a project in Visual studio 2010?
- How to add new item in the project?
- Basic understanding of entity data model.
- What is the purpose of global.asax file?
- Basic understanding of SQL.
Steps Involved
- Create a new project of type ‘ASP.NET Dynamic Entities Web Application’.
- Create tables at the backend
- Add a new entity data model
- Add code to the global.asax.
- Run the application
Step 1: Create A New Project Of Type ‘ASP.NET Dynamic Entities Web Application’
- Start Visual Studio.
- Go to File - New Project
- ‘New Project’ dialog box will be displayed.
- Select ‘Web’ under ‘Installed Templates’
- Select ‘ASP.NET Dynamic Data Entities Web Application’.
- Provide ‘Name’, ‘Location’ and ‘Solution name’ as shown below:
Click to Enlarge
Step 2: Create Tables At The Backend
Click to Enlarge
Click to Enlarge
Click to Enlarge
Click to Enlarge
Step3: Add A New Entity Data Model
- In the solution explorer, right click on project and click on Add - New item.
- Now select ‘ADO.NET Entity Data Model’.
- Give it a name which in this case is ‘Model1.edmx’ as shown in figure below:
Click to Enlarge
- Click ‘Add’ button.
- 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.
Click to Enlarge
- Click ‘Next’.
- In the next step you have to ‘Choose Your Database Connection’.
Click to Enlarge
- Click on ‘New Connection’ to open the ‘Connection Properties’ of your database. A Window appears as shown in figure below
Click to Enlarge
- Enter details of ‘Server name’ and ‘database name’ and click Ok. This will take us back to ‘Entity Data Model Wizard’ as shown below
Click to Enlarge
- Click next
- Select Tables and click Finish
Click to Enlarge
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
Click to Enlarge
Click to Enlarge