Database First + MvcScaffolding = Controller and Views

Apr 11, 2011 at 8:26 PM

With a few tweeks this appears to work flawlessly. Perhaps this was meant to be; or then again maybe not.

We started using code-first and then passed the model off to our webforms guys who added some custom controls and generated the main application. Subsequently, we want to add an administrative layer; however, the model has sinced changed and our code no longer work without allowing it to drop and recreate the database.

So we created a new application using Database-First, then scaffolded controllers, repositories and views for each of the generated classes. Since the MvcScaffolder generates it's own Context class, we needed to delete that file and then replace the reference to it in each associated repository class with the context created by the Entity Framework  (i.e. yournameEntities). Once these changes were made, things just worked.

So there are a few questions that come to mind; especially, since I haven't seen anything on scaffolding with db-first:

  1. Did we miss anything?
  2. Is this the way it's supposed to work?
  3. Is there a way to alter the scaffolding so that it uses the existing context, instead of creating it's own?
May 4, 2011 at 5:09 PM

Yes MVC Scaffolding is fully customizable. I suspect you didn't read Steve's blog.  (http://blog.stevensanderson.com/2011/04/06/mvcscaffolding-overriding-the-t4-templates) If you didn't, I strongly recommend you review all related posts of his as you will be amazed how flexible this code generation tool is.

Aug 11, 2011 at 9:29 PM

I just wrote a blog post on this topic. See http://weblogs.asp.net/paullitwin/archive/2011/08/11/use-mvc-scaffolding-in-database-first-mode.aspx