We get the following screen, that displays the connection string. Click Next.
Since we need to deal with the table, we will just select the table. Had there been a requirement for the stored procedure, we would have selected the stored procedure. As of now, we are good to go with Employee table. Click Finish.
We get the following entity generated and we can view it in .edmx file.
Let us have a quick view at the auto generated class files in the application. Following is the Employee class generated, using scaffolding.
Following is the context class that would be used to establish a connection with the database.
Adding Controller
Let us add the controller. Right click on the controller. Add->Controller. Select MVC 5 Controller with the views, using entity framework. Click Add.
Since we have Employee class as our model, we will select the same under Model Class and for the context, we would use the context class, generated by EF (SampleDBEntities). Click Add.
ASP.NET MVC will now start the processing to create the controller.
When the controller is created, we can see that it has auto generated code in it. Let us check the code generated by the entity framework scaffolding template.
Browse the Application
It has created all the methods to perform CRUD operations. Let us now run the Application and check if it is working fine or not. Browse the Application. Use Employee controller and create action, as it is a default action to create new records (Refer to controller code). Add sample data and click create.
Creating New Record List of all Employees
We can see the newly added record in the list. Hence, the insert functionality is working fine. Let us check other CRUD operations as well. Click Edit.
Editing Employee
We will update the name of the employee that we have just created and hit Save.
Employee Details
Employee name gets updated successfully. To see the details of the employee, click Details link.
We get the details of the employee.
Deleting Employee
Let us check the delete functionality. Go back to the list and click delete. We get the following screen. Click Delete.
The record got deleted from the database. We have our earlier records from the table.
Summary
Hence, we have CRUD operations working perfectly in the Application. This is how we can implement scaffold template using the entity framework in ASP.NET MVC 5.