MVC3.1 Scaffolding Magic with Database (or Model) First , Not Just Code First

The MVC3.1 scaffolding that was released at Mix can auto-magically create an EF 4.1 DBContext class, the basic CRUD code in your controller and the relevant views all in one fell swoop. (Don’t forget the additional scaffolding tools that will build things more intelligently, i.e. with a Repository (http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/).

All of the demos of this, including my own [MVC 3 and EF 4.1 Code First: Here are my classes, now you do the rest, kthxbai] demonstrate the new scaffolding using Code First. In other words, just provide some classes and the scaffold will do the rest, including build the context class.

 I saw a note on twitter from someone asking about using this feature with an EDMX file instead of going the code first way. You can absolutely do that. Here’s a simple demo of how that works and I’m using the in-the-box template in the MVC 3.1 toolkit  — though admittedly, for my own purposes, I’m more likely start with the template that creates a repository.

 1) Start with a class library project where I’ve created a database first EDMX file.

step1dbfirstmodel

2) Use the designer’s “Add Code Generation Item” and select the DbContext T4 template included with Entity Framework 4.1. That will add two templates to the project. The first creates a DbContext and the second creates a set of very simple classes – one for each of the entities in your model.

 step2codegendbcontext    

       step2dbcontextclasses

3) Add an ASP.NET MVC3 project to the solution.

 step4 new mvc

4) Add a reference to the project with the model.

step5 ref model project

5) BUILD THE SOLUTION! This way the Controller wizard will find the classes in the other project.

6) Copy the connection string from the model project’s app.config into the mvc project’s web.config. Step 7 will complain otherwise.

step7 copy connection string

7) Add a new controller to the project. Use the new scaffolding template (Controller with read/write actions and views using EF) and choose the generated class you want the controller for AND the generated context from the other project.

step7 controller

That will create the controller and the views. The controller has all of the super-basic data access code for read/write/update/delete of the class you selected. It’s a start. Smile

step8 create

Almost ready to run but first a bit of house keeping.

8) The most common step to forget! Modify the global.asax which will look for the Home controller by default. Change it so that the routing looks for your new controller.

step9 globalasax

 9) Time to test out the app. Here’s the home page. I did some editing also. It all just works.

volia

I highly recommend checking out the alternate scaffolding templates available in the mvc3 scaffolding package linked to above.

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

11 thoughts on “MVC3.1 Scaffolding Magic with Database (or Model) First , Not Just Code First

  1. Please elaborate on "though admittedly, for my own purposes, I’m more likely start with the template that creates a repository."

    Which "the template"?

  2. I found I needed to bring step 7 forward to before 6 otherwise the code generation did not work. Also if you create the model project first you will need to (obviously – I never got this wrong honest) set the WebApp as the startup project 🙂

  3. Andrew, you’re right. By the time I wrote the post, I had forgotten that the controller template bitched about not being able to find the connection string (or something in there). I’ve moved it into the right spot. Thanks.

  4. Thanks for the Post !!

    I have an issue while creating models using "Add Code Generation Item", the poco classes are empty if the model had a inheritance or 1 to 1 relation.

  5. In case of inherited model, selecting the generated sub class would create parent class references in the controller. Is this a bug ?

  6. Dear Sir firstly i m thankfull to you that you provide a solution fro begineer in ur post http://msdn.microsoft.com/en-us/data/gg685489.aspx .But CRUD operation not performing plz plz plz help me .Create method not save data in database without any error and edit,delete ,detail method give the following error
    “”The parameters dictionary contains a null entry for parameter ‘id’ of non-nullable type ‘System.Int32’ for method ‘System.Web.Mvc.ActionResult Edit(Int32)’…………………………………………An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
    Parameter name: parameters “”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.