As I mentioned in my last post, I am going to dive deep into ASP.NET web development. I have watched several video tutorials including Introduction to ASP.NET MVC by Christopher Harrison and Jon Galloway and Implementing Entity Framework with MVC.

One thing that I really like about ASP.NET so far is that Visual Studio is able to generate code for me which I would have to write repeatedly by myself. A feature called Scaffolding creates a Controller and several Views on basis of a model.

How does this feature work and how can we tell Visual Studio to generate the desired code for us? I’m going to tell you about it in this post.

Furthermore, I have come across some common errors that stopped me from generating the desired code. I also want to help anyone struggling with this feature to be successful.

How do I use scaffolding in an ASP.NET MVC application?

There are two prerequisites that we need to provide:

  1. We need a model class which acts as the basis for code generation
  2. We need EntityFramework installed in our project
  3. We need a DbContext class

First of all, you need to create a model class. A model represents the data in the MVC (model, view, controller) pattern. In this post, I am going to use a Person model implemented like this:

PersonModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class PersonModel
    {
        [Key]
        public int PersonID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public DateTime Birthday { get; set; }
    }
}

After creating your model, we need to implement a DbContext class for our project. In this case, I made it simple, because the DbContext is not the main topic of this article:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using WebApplication1.Models;

namespace WebApplication1.DataAccess
{
    public class WebApplication1DbContext : DbContext
    {
        public DbSet<PersonModel> Persons { get; set; }
    }
}

When both our model and our data context classes are implemented, we can go to the Solution Explorer and open the context menu on the Controllers folder of our solution:

Scaffolding Menu Item

On the next screen, we can choose what we want to generate. We select the MVC 5 Controller with views, using Entity Framework option:

MVC5 Controller Scaffolding

Finally, we arrive at the Add Controller dialog where we can fill in the model class and the data context class we implemented before. Make sure to select a model and a data context class. Otherwise, the Add button of the dialog won’t be enabled and you cannot click it.

Add a Controller

Visual Studio starts scaffolding when we click on the Add button and may successfully generate the code for our Controller and Views.

If there is a problem while the process is taking place, and there are chances, jump to the last section of this post where I write about common problems that occur while scaffolding.

Generated artefacts

The generated controller contains code for creating, editing, deleting and listing Person objects. With just a simple click in the scaffolding dialog, we have a full-featured CRUD controller.

Furthermore, Visual Studio generated the corresponding View to our Controller. There are the following Razor syntax views generated: Create.cshtml, Delete.cshtml, Details.cshtml, Edit.cshtml, Index.cshtml.

Just to show what a generated View looks like I show a screenshot of the Create.cshtml here:

Create View

Source code

You can find the complete ASP.NET MVC project for download here. It contains all the code shown above and runs after downloading the required packages from NuGet.

Common problems

The Add button in the Add Controller dialog remains disabled

The dialog does not communicate what the required fields are. We have to fill in a model class and a data context class. If we don’t provide one of those values, the Add button won’t be enabled.

Error: There was an error running the selected code generator: Try rebuilding the project

If we get the following error message, we are told the rebuild the project. This is odd because when we open up the dialog again, we have to fill in all the fields again. So make sure you always build your project before you try to scaffold your model into Views and Controllers.

Error Rebuilding Project

Error: EntityType ‘PersonModel’ has no key defined. Define the key for this EntityType.

Another common error is the following:

Error no key defined

When we face this error, it means that we don’t provide an “ID” column for our model. In the example above, I named my key field “PersonID”. If I want to do so, I have to add the KeyAttribute to the property to tell the system that this property should be the key field of the model.

[Key]
public int PersonID { get; set; }

Conclusion

Scaffolding is a powerful concept provided by Visual Studio which speeds up development and helps following design guidelines at the same time. I have used it since I started developing for ASP.NET MVC 5 and it helps me a lot.

At least with Visual Studio 2013 Update 4, there are a few things which could be improved. Anyway, I recommend trying it and see if it helps.

Share this

Claudio Bernasconi

Claudio Bernasconi is a professional software developer, Microsoft certified technology specialist, blogger, technology enthusiast and founder of CarParking Schweiz. Since 2010 he works for KMS AG in Lucerne, Switzerland.