How important is it to automate your development?
Very important! This is one trait that separates good developers from great developers. The less time you spend writing repetitive code the more time and energy you have to solve the challenge problems.
If you have ever used the MVC framework in your development you know the routine you create a model with your Getters and Setters, then your controller so you can access the data and finally the view for each page so the user can use your app. This assumes you already have your database in place if not you need to create that as well. With Scaffolding you can create all of these at the same time and move on to solving the big issues.
Scaffolding is an outline auto-generated from the application’s Model. By using scaffolding for a Web Application we can set up our Model and begin using our application. The application will be very basic but fully testable in a matter of minutes. Both C# and VB support scaffolding and it is fully shipped with Visual Studio 2012.
Scaffolding with MVC 4.0 Tutorial
For this example I will use an application I am creating for tracking books. My development environment is Visual Studio 2012 Express with the C# MVC 4.0 template.
- Add a new model with your Getters and Setters declared. In my example I have 5 field for now that I want to track an ID, Book Name, Date Read, Review and Author
- Next move to the Solution Expl0rer and right click on your Controllers folder then select add new Controller
- Visual Studio will open up a dialog with five data fields:
- Controller Name – This is the name of your Controller BooksController
- Template – Specifies the template Visual Studio will use to populate your Controller MVC controller with read/write actions and views, using Entity Framework
- Model Class – The Class name and Model to populate template data Book (BookTracker.Models)
- Data Context Class – The Database Connection String named in your Web.Config file BookContext (BookTracker.Models)
- Views – Templates for the Views, Razor is the default Razor (CSHTML)
- After clicking the Add button Visual Studio will automatically populate the Controller and corresponding Views.
Wow in just 4 quick steps Visual Studio saved us at least 20-40 minutes of setting up our Controllers and Views for edit, create, details, delete, and index for my application. So basically with using the MVC template in Visual Studio 2012 and Entity Framework Scaffolding we have created a CRUD application in about 5 minutes.