Dave on Facebook Follow StationaryDave on Twitter
Dave's profile on LinkedIn
My Skype Status
TechQuest: Welcome to TechQuest.
Lickety-Split Coding with Code First MVC 4 Scaffolding

If you’re a software developer working on existing projects, you may not have many professional opportunities to use some of the latest whiz-bang tools and technologies that are out there. I am building out the www.CurbstoneComment.com web site, so I get to do green field development--build from scratch. I thought I’d take the “code first” approach to database design/development, letting entity framework build my database for me. I found this helpful article that takes the concept one step further by using MVC 4 Scaffolding to do the actual coding of my project’s Controller and View classes. I patterned my work after the steps it outlined.

Technologies Used Here:

  • Visual Studio 2010 with SP1
  • C#
  • MVC 4
  • Entity Framework 6
  • Nuget Package manager for Visual Studio
  • Sql Server 2008

Before We Start

  • Code first is the process of designing and implementing a database by first creating Model classes that describe the database. Then Entity Framework 6 reads your classes and automatically generates the SQL Server database tables. You can read more here.
  • Entity Framework is an object-relational mapper that enables developers to work with relational data using domain-specific objects. What this means is developers don’t have to write by hand much of the code for database access. You can read more here.
  • MVC 4 is a framework for building web applications. Our web application for CurbstoneComment.com uses MVC 4. You can read about how we built our application up until this point here.
  • MVC Scaffolding is the newest and most obscure of these technologies. Scaffolding takes the auto-generation of code one step further and creates Controller and View classes based on the Model classes used to create the database.

Ready!

We begin by installing MVC Scaffolding. This is a NuGet package installed via VS 2010’s Package Manager Console. For details on how to install these packages, read the first article referenced above here.

Then we build our Model classes, using decoration that will help Entity Framework build its database tables. The ItemModels.cs file will include classes for Item, ItemCollection, and CollectionType.
Uploaded Image

The magic happens when we issue the Scaffold Controller Item –Repository command within Package Manager Console. Scaffolding created views for my Item objects:
Uploaded Image

And Scaffolding created a controller class for our Item objects:
Uploaded Image

I just had to add links to my menu and the code was ready to run.
Uploaded Image

But my database tables aren’t created yet. The tables don’t get built until the application is run. This happens when the global.asax file’s Application_Start event fires and issues the command System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges());

The first time any user clicks on the Items link, there’s a short pause and there you go, your tables are created!
Uploaded Image

But beware: if you make any changes to your model, your database tables will be dropped and re-created, meaning all of your data is hosed. If I were using this in a production environment, I would want to make sure I understood all the circumstances that would kick off a rebuild of my database tables. It’s easy to see how this could be catastrophic for a running web site. It would be nice if EF creates a restore script before it wipes out your data.

I also noted a failure in one of my tables. I had tried to create a table ItemCollection that included a list of Ids back to individual Items. It simply ignored this field. It could have set up a link table with ItemCollectionIds and ItemIDs but I guess even Entity Framework 6 is not that smart.

Overall, these are powerful and time-saving tools for developers. I hope this helps someone!

8,153 views.