Use MVC Scaffolding in Database First Scenarios

Steve Sanderson's MVC Scaffolding NuGet package is awesome and goes far beyond the very good scaffolding available from the Add Controller dialog in Visual Studio 2010 (after installing the MVC 3 Tools Update).

But a shortcoming of MVC Scaffolding is that, out of the box, it works only in Code First mode. However, with a little finagling you can get it work in a database first scenario. I'm sure there are several different ways to do this but here is what I did that was pretty easy. This assumes the database schema has already been created using SQL Server.

  1. Add an ADO.NET Entity Data Model to the Models folder of your ASP.NET MVC 3 application.
  2. Select Generate from database from wizard and generate the entity model for your database objects.
  3. Right-click on the .edmx model file and select Add Code Generation Item... from the pop-up menu.
  4. Under Code tab of dialog, select ADO.NET DbContext Generator. This changes the EF model to use DbContext instead of ObjectContext and creates a DbContext class for the database, by default, with the name databaseEntities. For example, I have a database named BPath. So for this database, EF creates a DBContext class named BPathEntities. This step is key because it will make the database-first classes you generate compatible with the code-first classes that MVC Scaffolding generates later.
  5. Now use MVC Scaffolding to generate the controllers for your app utilizing the entity classes that the DbContext generator generated for you in step #4, which, by default, will have the same name as the tables/views from your database that you selected in step #2.
    For example, in my database I have a table named Participant so I would enter
    Scaffold Controller Participant -force -repository
    into the Package Manager Console to scaffold a controller and views for the Participant entity from the model. The -force option overwrites any existing items and -repository creates repository classes for the entity.
  6. Now open the repository class that MVC Scaffolding created. It will be named, by default entityRepository. So in my example, I would open ParticipantRepository in the code editor in Visual Studio.
  7. Now you will note that MVC Scaffolding created a different DbContext for itself named, by default, projectnameContext that it references in the ParticipantRepository class.
  8. Change the context statement so it now uses instead your db-first context instead of MVC scaffolding's code-first context.

In my example, I have commented out the code-first context named BPathMVCContext and replaced it with the database-first context named BPathEntities:

ParticipantRepository Class Changes

Repeat the process for any additional controllers and you are done. Hope this helps.

10 Comments

  • What do you do if your database schema changes?

  • 1. Install MVC 3 Tools Update.
    http://weblogs.asp.net/scottgu/archive/2011/05/03/asp-net-mvc-3-tools-update.aspx

    2. Install MVCScaffolding from NuGet.

    3. Right-click on Controllers folder and select "Add Controller".

    4. Select "MCVScaffolding: Controller with read/write actions and views using repositories".

    5. Make sandwich and celebrate. :)

    Also note that you need to install EF 4.1 on your system to have access to the DbContext Generator. It's not enough to an a reference to EntityFramework via NuGet.

  • @mattslay: I find the easiest thing to do is 1. Create backup of project. 2. Delete the entities that changed form EDMX . 3. Right click on EDMX surface and select Update model from database and add those updated entities back. 4. Regenerate controllers, views, repositories as needed. 5. Copy any customizations you had made to controllers/views/repositories from the backup version of the project to current project.

  • ,re: Use MVC Scaffolding in Database First Scenarios,,,

  • As with most things that comes out these days. It all seems great in principle but when trying to get it to work it all falls over. Tried following the instructions but just can't get this working, seems that there must be a whole more involved.

    Will have to keep banging on. Thanks for a starter for 10

  • @Gary: hmmmm. I have gotten this working following the above steps a bunch of times so I am surprised.

    Are you using repository classes?

  • I get all the way to the last step but I am getting this error when trying to view newly created controller?



    The type 'GMS_Sandbox_MVC.Models.Organization' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.

  • Using visual studio web developer 2010 express this worked like a charm for me. Thanks very much for posting. I'm new to web stuff in general, and the code first approach felt like I was going down the wrong path, as cool as it is that you can do it, I feel more in control with the database first.

  • This was just what I needed. I played around with the concept on my own for a while and gave up. It was the swapping of the context (entities) that made the difference for me. Thanks.

  • Excellent Stuff..
    One can also use the DbContextType switch on command line.
    e.g. using command
    Scaffold Controller Participant -force -repository -DbContextType "BPathEntities"

    in step 5 gets rid of Step 8.

Comments have been disabled for this content.