Tuesday, February 5, 2013

Part1: Migrating from EF 4.0 to EF5.0

Warning! Multiple blog post article. I would advise not proceeding until all the articles are done.

Since it is a patch Tuesday for me I figured it would be a great time to upgrade my EF instance from 4.0 to 5.0. Please note these are my own musings.

Prerequisites: Install .Net 4.5 on Windows 7 available at Microsoft's website: here. Patch your rig with latest windows updates Watch progress bars (optional) Drink red bull (recommended) Verified all files are checked into TFS Made full backup of SQL Server (Both the TFS DB and the website DB).

While I was waiting I decided to look for a new visual style for VS2010 since the monitors on my desk were always using a light background leading to headaches and eye strain. I ended up settling on "The Dark Side of Visual Studio" which you can download yourself: here. Full credit to InfinitiesLoop for creating this awesome theme.

32 minutes later... More rebooting and another 16 minutes of patching.

Installing Visual Studio 2012 which requires almost no explanation (just lots of moving dots while it installs). Swell.

If your using a TFS open visual studio 2010, copy your settings and port them to 2012. Your local folder mapping should remain the same.

Start upgrading your project properties and select 4.5 as your framework target. In your web.config go to the compilation element targetFramework=”4.0″ and replace it with 4.5.
I also added httpRuntime targetFramework="4.5" right under system.web and before compilation.


Now for the entity framework. More than likely once you did this a nice warning popped up:
Warning: Old school doesn't like the new school kids.


Lets add the new 5.x .tt to generate our new functionality so once we are done we can get rid of the old one. You backed up your TFS before this right?
Rename your current 4.x with an "Old"  suffix in case things don't work out.
Update the $edmxInputFile$ variable with the name of your edmx model.
In my case I used Z10.edmx

Next perform the same steps on XXX.Context.tt. Below our two sample screenshots I saw in my application.

Sweet! I have all of my new DbSet<T> objects. 
Due to my design I had implemented my custom logic via partial classes in a separate file (called ModelExtensions.cs). With this methodology the template engine can regenerate as much as it sees fit without killing my extensions and thus I can remove the Old Template with out feeling guilty (you did make a backup right?). Scalpel.

This is the critical part. In my striving effort to make this upgrade I want to start making sure that I will implement an actual repository. Before I hadn't bothered setting one up since it was a small application I had made for my personal use. Now it is time to get my refactor on.

No comments:

Post a Comment