MVC3 Mapped to ASP.NET

By candice

Following on from the DDD Guathon I attended back in June I’ve finally got the chance to get to grips with MVC3 – specifically EF Code First using Razor and HTML5 setup. I’ve decided to have a go at working up an application that can also be used as a mobile website. For now though I needed to get my head around MVC3 …

First stop was the asp.net website which is great for step by step tutorials – check out this one for MVC3 EF Code First: http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Casting my mind back to the Guathon I could kind of remember how things worked but being quite a visual person I figured if I try to ‘map’ the MVC architecture to ASP.NET I might get my head around things a bit quicker.

Fortunately when setting up an MVC3 project in Visual Studio you get a bare bones application ready made, so I used this to start my ‘mapping’ process. Here’s what I found:

  • Views\Shared\_Layout.cshtml  = Master.Page
  • Views\Shared\_LogOnPartial.cshtml = UserControl
  • Views\Account directory has an associated Controller class Controllers\AccountController.cs = Code Behind for each page in the directory
  • Models seem to deal with Views containing forms and contains the form validation/labelling, plus any interaction with the database.
  • Database access code is still held in a DAL but a folder rather than perhaps a linked application and its structure is somewhat different. Here you create a Context class, which is referenced by the root Web.Config file for communicating with your database, and an Initializer class, if you need it – handy when using the Code First model for creating your application since this class allows you to setup your test data one time. Then every time your code changes and requires a database build it injects this test data keeping your application running through development.

My next challenge, bearing in mind I’m developing using Code First, was to locate the database – again being quite visual I like to have my database accessible in SQL Server Management Studio.  I had started to extend the application and had set the Web.Config file up with my additional Context reference with the same details as the ApplicationServices Context. This created a .sdf file under App_Data which was great – I could access it in Visual Studio for now – but what about when I deploy and need to make changes to it without using Code First? So I updated to Web.Config for my new Context to reference one of our main SQL servers to create my database there. I needed to use the central SQL login in to gain immediate permission on the Master database to allow MVC to create the database for me but then I was rocking. Unfortunately the ApplicationServices would not migrate to this location, I would have liked it to sit on the same location but for now it’s not the end of the world.

The great thing was that in addition to creating the database structure for the extended area of my application MVC also rattled out the scaffolding I needed to get this area of the application running. Very neat and very quick!

Related posts