Do you find yourself repeating the same set of arduous and potentially error-introducing tasks each time you sprint or create a new project? Do you work with databases and typically write code to link up the data model to EF/Code and then do additional plumbing for managing entity relationships in your views and controllers? What about consistent validation across the EF code first to the controller and the views?
This article assumes that you have installed the latest MVC 3 bits into your development environment. I am using Visual Studio 2010 Ultimate. If you haven’t installed the latest MVC 3, you should do that first by going here: http://bit.ly/jnsOR0
In an over-simplification, MvcScaffolding is to your web application, what Code Snippets is to your document (code, xml, etc.). In other words, using MvcScaffolding you can, for instance, automatically generate
1. Database contexts from your model,
2. Controllers for creating, viewing, updating and deleting instances of your model or your viewmodel,
3. Corresponding Views for creating, viewing, updating, and deleting data,
4. Catalyze the generation of a database model including support for one-to-many relationships and more…
In addition, you can customize your own scaffolding for instance to provide for default Localization support, custom authentication/authorization support, etc.
In this article we will walk through the creation of a read/write/update/delete of Recipes and its related (many) Ingredients using MvcScaffolding and MVC 3.
Start your engines…
(often-times, the launch of Visual Studio seems like it given the time that it takes)
Step 1. Ensure that your development environment (again, I am using Visual Studio) has the latest MVC 3 bits installed. See above if you haven’t done so already.
Step 2. Create a new MVC 3 Project

Choose the ASP.NET MVC3 Web Application Template as shown above,

I selected the Intranet Application option, you may use Internet or Intranet. Make sure that you are using the Razor View Engine and continue by clicking on OK.
Step 3. Deploy the latest MvcScaffolding
Now you are ready to deploy the latest MvcScaffolding package to your solution. To do this,
First launch the Package Manager Console

You will see the console with a PM> prompt. Type the following as shown below
PM> Install-Package MvcScaffolding

You should then get the following results (you must be connected to the internet in order for this to work).

Step 4. Create your models
Here we are using Entity Framework 4’s code-first magic to first create classes for our entities.
Let us start by creating a class for Order (many side of the one-to-many relationship).

Let us now create a class for Customer (one side of the one-to-many relationship).

You will notice that we have included a foreign key for Customer in the Order class. Also, we have included a virtual collection of Orders in the Customer class to represent the orders placed by that customer and likewise, we have placed a virtual Customer member in the Order class to represent the customer for that order. You will see shortly how this plays out.
Step 5. Scaffold your models
Now the magic begins. For each of your models (Order, Customer) use the Package Manager Console to scaffold out the database plumbing, controllers and views.
Let us start by scaffolding the Order model by running the following
PM> Scaffold Controller Order –For ce

Now do the same for the Customer model.
Step 6. Compile and run your application
…the moment of truth. Run your application and browse to the Customers controller default view as shown below

Create a few customers (notice how the validation is working)

Now create a couple of orders for one of your customers. (Notice how the drop down brings in the customer names to choose for the order. Nice eh?)


Now list your orders (Notice how it shows the customer name for each order)

List the customers (Notice how it shows the total number of orders for each customer)

Browse away and enjoy.
Notes.
I hope that you have found this useful. Scaffolding is a powerful concept to quickly build out functionality to your MVC applications. Maybe you can customize the scaffolding to include internationalization/localization support and share it back with us? I have attached the source code for this project.
Downloads
Download Source Code (2.68 mb)
Download as PDF (941.45 kb)