Wire up DB

Apr 12, 2011 at 3:53 PM

As on old Rails guy, McvScaffolding fills in what ASP.NET MVC has been missing. Question though, how would you wire up an existing DB (with or without models)

Apr 21, 2011 at 4:21 PM

In our case, we needed to work with an EF created database that had been modified by our SQL guys and put into production by other devs using webforms to accommodate some custom controls. Our task was to create an administrative app for that db. We used EF Database-First, to generate the classes from the revised db and then used MVC to scaffolded the controllers, repositories and views for each. Scaffolding creates an alternate context and so we needed to delete that after adding each DbSet<> to the context created by EF Db-first.

You could just as easily use EF to generate the classes from an existing model, then delete everything except the classes and use FE Code First from that point on. When you generate classes from the db, Solution Explorer shows them as links in a hierarchial view. Ignor that, they are still individual files inside the Model directory visible to Windows Explorer.


Apr 21, 2011 at 4:56 PM

Thanks for your thoughts. In the end I went with rolling my own scaffold controller template. I approach MVC projects with the intent of keeping thing DRY and lean. I do not like fat controllers. All controllers inherit from a BaseController that connects to the service layer where validation and other business logic takes place. The service layer connects to my repository layer when I do all DB work.  I stick the service and repository CRUD methods at the base of the controller file and then just cut/paste these into their corresponding files. It's not the best solution, but it works. I'm also working on a scaffold template that will EDMX file and generate my service and repository for me. Currently stuck on opening and modifying an existing file; example, I would like to be able to open an existing repository.cs file add new methods and save it. If work out the solution I will post it here. Thanks again!