Recently, I finished upgrading my ASP.NET MVC 4 application(in Visual Studio 2012) from .NET 4.0 to .NET 4.5 framework. Not-so-easy job though. A lot of pain and anxiety is involved when you migrate to a different target framework version and when you know that you have a lot of Nuget package references and use a couple of third-party components that interface with a specific version of .NET framework. So, i thought of sharing my experiences and challenges during this process.
The easiest way to target all projects to .NET 4.5 framework is by doing a search for all *.csproj files and replace <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> with <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> (or client profile, etc). You would also need to replace the <compilation> element's targetFramework=”4.0″ with 4.5 in the application's web.config file.
Issues after migrating ASP.NET MVC solution from .NET 4.0 to .NET 4.5:
- The real issue are the NuGet packages, they will just not work, – some have a .NET 4.5 specific version – a good example is Entity Framework. When the project is targeting version 4.0 of the framework and you install/update EF to version 5 you actually end up with EF 4.4 (which doesn't have support for enum types and so on, targeted for .NET 4.5). When i looked at my project reference, I found the following entry:
<Reference Include="EntityFramework, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
The only way to solve this is to remove all packages and install them again, which is a massive pain to do manually and unfortunately the current version of NuGet is not of much help there either.
Open Visual Studio 2012,
Go to “Tools” > “Library Package Manager” > “Package Manager Console”. This will open a powershell command prompt,
then type: uninstall-package EntityFramework for each of the listed projects for the default project dropdown.
After uninstalling EntityFramework from each of the projects, install EntityFramework by typing “install-package EntityFramework” in the powershell command-prompt.
NuGet 2.0 doesn't handle re-targeting your applications very well. In order to change your packages' target frameworks, you must uninstall and reinstall the packages.
The reasons why packages must be uninstalled and re-installed are:
- When installing a package, we determine the target framework of your project
- We then match that up with the package contents, finding the appropriate \lib\ folder (and \content\ folder)
- Assembly references are added with Hint Paths that point to the package's \lib\ folder, with the right subfolder (\lib\net40 for example)
- Content files are copied from the packages \content\ folder, with the right subfolder (\content\net40 for example)
- We record the targetFramework used to install the package within the packages.config file
- After you change your project's target framework, the Hint Paths still point to net40
- When you uninstall packages, we check the targetFramework that was recorded in packages.config to see what target framework's libs/content to remove from your project
- When you reinstall the package, we detect your updated target framework and reference/copy the right libs/content
2. ELMAH and Ninject work like a charm without any changes. That's a good news.
3. Make sure you take a look into this one about the new Date and DateTime editor templates in MVC 4. Also there seem to be model binding changes to do with DateTime .
4. Lastly, don’t forget to update your test assemblies App.config with the correct assembly redirects if you are running tests against ASP.NET MVC controllers, which use MvcFutures or MvcContrib, because you will end up seeing weird “Type A doesn’t match type A” errors.