This article walks you through using ASP.NET Scaffolding to generate controllers and views. 


Using MVC and Entity Framework, you can create a web application that provides an interface to an existing database table. This demo shows you how to automatically generate code that enables users to display, edit, create, and delete data that resides on the database. The generated code corresponds to the columns in the database table.  Unless you have very simple requirements, the scaffolded application is probably going to be a prototype rather than complete and ready to deliver to end users.

STEP 1 - Create Project

Create a new project of type ASP.NET MVC 4 Web Application  

Choose the option Internet Application

After project creation, it look like this:


STEP 2 - Add Items

Add new items of type ADO.NET Entity Data Model to the solution.

Name it to Model.edmx


STEP 3 - Create Model

Open the Model.edmx, with double click on the top of the file and create the data structure.


On this sample, we have two entities (Clients and Contacts).

After save this model, two new classes will be generated (Clients.cs and Contacts.cs)






STEP 4 - Install Nuget

On the package manager console, execute the follow command.

Install-package mvcscaffolding 

STEP 5 - Scaffold Controllers

Now we can scaffold a controller and a set of Create, Read, Update and Delete (CRUD) views. In the Package Management console run the following command.

Scaffold controller ClientsController –force –DBContextType “ModelContainer”

Scaffold controller ContactsController –force –DBContextType “ModelContainer”

This will generate a set of views, a controller and an Entity Framework database context.


On the next image, you can see the files created by the command executed.

The repositories, controllers and views.

STEP 6 - Change ModelContainter

Comment the line //throw new UnintentionalCodeFirstException()


STEP 7 - Change Menu

Create two new entries on the application menu.

One to call the Clients entities and other to call Contacts entities. This could be made on the /Views/Shared/_Layout.cshmtl.


STEP 8 - Run Application

Run the application.

Verify that two new entries exists on menu and that you can Create/Edit/Delete both entities created.


MVC4 Resources

Some good online resources about MVC4: 

Code Samples

All of this sample can be found and downloaded in Microsoft Code Gallery: