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.
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.
3) Add an ASP.NET MVC3 project to the solution.
4) Add a reference to the project with the model.
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.
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.
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.
9) Time to test out the app. Here’s the home page. I did some editing also. It all just works.
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”
Please elaborate on "though admittedly, for my own purposes, I’m more likely start with the template that creates a repository."
Which "the template"?
the template inside of the scaffolding package that I mentioned and referenced in the first paragraph. 🙂
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 🙂
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.
Thank you for all you do.
I am reading your Programming Entity Framework 2nd edition and learning much.
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.
In case of inherited model, selecting the generated sub class would create parent class references in the controller. Is this a bug ?
This is my sample data from database,
ID Name ImageUrl
1 a notous.blob.core.windows.net/…/1-9.jpg
2 b notous.blob.core.windows.net/…/10_discount-150
3 c notous.blob.core.windows.net/…/FB-button-341×3
I want display this data not for same This ImageURL display as image in My view Using ASP.NET MVC,
And also At the time of insert also after uploading the image save in database Like above(its possible???)
please help me,I am new to this MVC
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 “”
Try this: http://stackoverflow.com/questions/18052813/the-parameters-dictionary-contains-a-null-entry-for-parameter-id-of-non-nullab