Scaffolding in ASP.NET MVC generates the code for create, read, update and delete operations in your application. This post outlines the different Scaffolding templates available in MVC4. If you do not like the default scaffolding then you can customise the code generation.You need a model to start working with scaffolding so build a model first by creating new ASP.NET MVC4 project in Visual Studio 2012 and select Internet Application template. Read this post to understand the each application template purpose in below window.

Now add a Scaffolding controller by right-clicking on controller folder in your solution and select Add Controller, A dialogue box appears for setting the controller name and scaffolding options.

After clicking Add, Scaffolding places all views and controllers in the project. Four different types of Scaffolding templates are available in MVC4, Each template controls the code generation.

Empty Controller

When you select this template, an empty controller class will add to the Controllers folder with the name you specify. It contains an Index method with no code and it will not create any views.

Controller with Empty Read\Write Actions

When you select this template, it add a controller with Index, Details, Create, Edit and Delete actions. You have to add your own code to these actions and create the views for each action.

API Controller with Empty Read\Write Actions

When select this template , it adds controller that derived from ApiController base class. You can use this template when you want to build a Web API for your application. What is Web API can be read here.

Controller with Read/Write Actions and Views, Using Entity Framework

When you select this template, it then generates a controller with Index, Details, Create, Edit and Delete actions and also generates the required views and code to retrieve the data from a database. Scaffolding basically examines the model that you have selected and uses that information to build the views,actions and database access code.

DbContext Class

Scaffolding needs a Data Context Class which you can select from the drop-down list or you can create new Data Context class. The generated DbContext class looks as below

ASP.NET MVC4 automatically includes a reference to the Entity Framework. EnityFramework supports code first approach, where do not create the database first for your application. In this approach EF uses DbContext class. You can notice that ProductDBContext class added to Model folder. To access the database then you need to instantiate this ProductDBContext class.

ProductController

The generated ProductController in the controller folder will have the code for select and edit product information. Part of the code in controller looks as below

The View takes the model and uses it in for each loop to display product information. In the Entity Framework code first approach, If you do not configure the database connection to use at run time, Entity Framework creates a one for you in LocalDB instance of SQL Server Express. Run the application then you will see something like this in below screen

Scaffolding only gives you a starting point for building an application. You then can customise the screens and code. If you do not like the default code generation or templates then you can find new templates in NuGet repository. Scaffolding just saves you time by adding common files to your project.