Sunday 18 December 2011

MVC3 EntityFramework POCO StructureMap

The purpose of this post is to explain how to setup an MVC3 solution which uses Entity Framework POCO as an Object-Relational Mapping (ORM) tool and StructureMap as an Inversion of Control (IoC) tool.

Step 1 - New MVC 3 Project

There are plenty of resources available to show you how to start a new MVC 3 Project in Visual Studio 2010 so i won't go into that...

Step 2 - Add Entity Framework 4.1 as your ORM

There are a few different ways to do this but i use the NuGet package which is pretty straight forward.

  1. Open your MVC3 project in VS2010
  2. Tools > Library Package Manager > Manage NuGet packages
    • Click 'Online' in the left hand menu so you can search for new packages
    • Search for 'Entity Framework' - at the time of writing, version 4.2 was available but you can also go direct to the nuget site and some previous versions may be available, for instance, i'm using version 4.1.10331.0
    • See http://nuget.org/packages/entityframework
  3. Simply click 'Install' button and the framework will be installed your machine for future use
  4. Now you have EF installed, right-click on the 'Models' folder (or where-ever you want your .edmx file to end up) in your solution and choose Add > New item
    • Under the 'Data' heading, choose 'ADO.Net Entity Data Model'
    • Follow the prompts accordingly...use Google for more info ;-)
Step 3 - Install and configure StructureMap

  1. You can get the latest version of StructureMap from here: http://sourceforge.net/projects/structuremap/ 
  2. The easiest way to install and configure Structure Map though, is to use the NuGet package manager again
  3. Similarly to step 2.2 above, search for 'structure' within the Online NuGet packages and you should find one (at the time of writing) called 'StructureMap.MVC3'
    • Be sure not to confuse this with another very similar package called 'StructureMap-MVC3' which is now deprecated
to be continued...

No comments:

Post a Comment