Saturday, 18 January 2014

ADO.NET Entity Data Model in ASP.Net Scaffolding

The ADO.NET Entity Data Model designer provide a Graphical User Interface tool to design the Entity Framework Models. We'll create and modify the entities, associations and inheritance relationships with the Entity Designer and we can also validate the model.
In that context, we'll today work on this session to create an Entity Model and use it in the application.
So, let's proceed with the following sections:
  • Application Creation
  • Working with Entity Model
  • Working with LocalDb
Application Creation
Create an ASP.NET Web Application in the MVC Project Template in Visual Studio 2013.
Working with Entity Model
Step 1: Just right-click on the Models folder to add an ADO.NET Entity Data Model named College.
Step 2: In the next wizard, select "Entity Model".
Create Entity Model
Step 3: The designer opens a blank model in which we add the entities. Just right-click on the designer to open "Properties".
Data Model Properties
Step 4: Set the Entity Container Name to CollegeDbContext as shown below:
Entity Container Name in Properties
Step 5: Right-click on "Design" then select "Add New" -> "Entity...".
Adding New Entity in DataModel
Step 6: In the next wizard enter the entity name as College and CollegeId as the key column name and click "OK".
Add Entity in Model
Step 7: Right-click on Entity to add a Scalar Property to it.
Adding Scalar Property of Entity
Step 8: Create the Name property also. Now add an another entity name Student and do the same as done above.
Step 9: Now right-click on design then select "Add New" -> "Association...".
Add Association in Entity Model
Step 10: Ensure that your wizard looks as below and click "OK".
Adding Assocaiation of Model
Working with LocalDb
Step 1: Open the Generate Database from Model by right-clicking on the design surface.
Generate Database Model
Step 2: In the next wizard click on New Connection and do as shown below:
Add Connection with LocalDb
Step 3: Click "OK" and if the wizard opens to create a new database, select "Yes".
Step 4: Select "Next" and the designer calculates to create the script of the database schema. Ensure that the Entity Connection Settings in the Web.Config option is checked.
Generate Database Wizard
Step 5: Click on "Finish" and the script opens. Right-click and select "Execute".
Execute Script of Database Schema
Step 6: Build your project. The Entity Designer creates two classes named College and Student and a model is added to your Models folder.
Step 7: You can now use the new ASP.NET Scaffolding as usual for the EDMX generated models. Now you can use your newcontext as a Data Context class while scaffolding as shown below:
Add Controller Scaffolding in MVC
Summary

This article will help you to create an Entity Model using the ADO.NET Model Designer and you can also use the new EDMX models in a ASP.NET Scaffolding. Thanks for reading.

No comments:

Post a Comment