Windows Phone 7 Deve Launch
# Thursday, October 04, 2012
« Unrecognized Expression Error in jQuery ... | Main | Some excellent free developer books–Succ... »

Moving an MVC4 Visual Studio 2010 project to 2012 is so far in my experience with a small application rather quick and easy. However, if that’s all you do then your app will be missing out on the nice .Net 4.5 and Entity Framework 5 features. 

Upgrading these two items, while not terribly difficult, does have a few challenges to overcome even for my relatively small application with two projects and about a dozen NuGet packages.

Hopefully my experience will help the next person get through their upgrade faster.

MVC 3 or MVC4 Release Candidate?

If you are on MVC3 or say the MVC4 Release candidate take a look at these extra steps to take.

MVC4 & VS 2010 to 2012 Steps

Open your VS 2010 solution in 2012 and let it convert your solution.

And done… Well almost.

If you happen to be using dependencies / nuget packages, there may be a chance that these are using older frameworks.  If you were already using MVC4 in 2010 chances are then you already figured out how to direct these packages to use newer versions of their dependencies using binding redirects. 

For example MVCContrib.TestHelpers references the MVC3 runtime.  My VS10 project was already using a binding redirect so all should be good. 

I am also using the MiniProfiler and in my MVC4, 2010 project I upgraded to the Entity Framework 4.4 Prerelease.  When I did this I had to add a binding redirect since the MiniProfiler at the time was using an earlier release.

If you are having issues, you’ll have to investigate if you have a dependency using an older version of a DLL that you can use a redirect to solve.

MvcContrib.TestHelpers***

In my case, my VS12 project with the TestHelpers stopped working even with the redirect.  I tried on my PC/laptop as well new and upgraded projects.  I can only assume it must be something wierd on my boxes.  I have been using the betas, previews and RC versions of MVC on both boxes so it just might be something related. 

The binding redirect *should* work.  However, if you run into my case, the atomic option is to download the MVCContrib source, change the reference for the TestHelpers project to use the MVC4 runtime, recompile and use the new DLL in your project.  This is what I ended up doing.

Done. If that’s all you want to do.

Upgrade Project(s) to 4.5

Right Click on each project and select properties.  Change the Target Framework to 4.5 in the Application section.

Update all packages to target 4.5

Perform a search and replace for the entire solution to replace targetframework=”net40” to targetframework=”net45”.  This will update all of your packages.config to use 4.5 targeted packages instead of 4.0.

targetNuget

Open your web.config and change your targetFramework=”4.0” to 4.5.  This informs IIS to also target 4.5 now.

Upgrade EF 4.4 to EF 5

Uninstall the EF package, and reinstall it via Nuget. I recommend doing this from the console/command line instead of the package GUI.  At the command line you can force an uninstall just in case you have other packages that require EF.  I originally uninstalled/reinstalled via the GUI and first removed all of my dependent projects and then added them all back in but I ran into problems with my dependent projects having issues.  I found it easier to force EF first, make sure it was all working and then investigate my other dependent packages one at a time.

You can open the console from the Tools Menu, Library Package Manager and Package Manager Console

nugetConsole

or by typing console and hitting enter in the Quick Launch which is in the upper right hand corner of VS 2012.

quicklaunch

The console opens in the lower middle of VS 2012 and allows you to type in nuget commands.

console2

Type in

uninstall-package EntityFramework –Force

and then

install-package EntityFramework

Be sure to do this for all projects using Entity Framework.  You can change this by selecting the project from the dropdrown list in the console.

If all of the above was done correctly it will install version 5.0.  If it still says 4.4 something is still pointing to the 4.0 framework.

efVersion

Build your solution.

If you get an error about the CompareAttribute being ambiguous reference…

CompareAttribute

Fix by Adding System.Web.Mvc. to the Compare Attribute.

Before:

CompareAttribute1

After:

CompareAttribute2

Rebuild.  Congratulations.  You now have a VS 2012 MVC4 project using .Net 4.5 and EF 5.

Now for the rest

From here you should now investigate all of your Nuget Packages or external references.  Since we are now pointing to 4.5 it may be that a newer version is available.  Nuget doesn’t handle updating to the new targeted version at this point.  You’ll have to look at each package and if you know it has a newer one for 4.5 you’ll want to look at uninstalling it and reinstalling as we did earlier.

One Quick way to reinstall all the packages

You could delete all of your packages and their folders and set Nuget to restore packages.

packageSettings

PackageRestore

Now the next time you recompile, Nuget will get all of the new stuff.

However, this could wreak havoc on your project.  I opted for handling my dependencies one at a time so I could deal with issues for each package one at a time. 

Happy Coding! Smile

Tuesday, October 16, 2012 3:03:20 AM (US Mountain Standard Time, UTC-07:00)
who can help me code share on Facebook,tiwtter.thanks
quang
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: b, blockquote@cite, em, i, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview