Downgrading an MVC4 project from .Net 4.5 to .Net 4.0
I recently created a new project in Visual Studio 2012 and didn’t really think much about the .Net version (mistake!). It turns out that the server it is destined for is still MS Server 2003, which will not run .Net 4.5. Oh dear.
So I Googled downgrading, and the process of changing the target framework from 4.5 to 4.0 is very simple, just a couple of clicks, but getting the project to build after that can be a major headache!
Changing the target framework:
- Right click on the project in the Solution Explorer (in Visual Studio) and select Properties.
- Change the Target Framework drop-down to whatever framework you now wish to target.
- Do the same for any other projects or test projects in the solution, that you also wish to change the target framework for.
Note: Make sure you do not select .Net Framework x Client Profile. This seems to have caused others problems.
Getting the damn project to build again…
This is where my headaches began. I only got a couple of errors when building, but they took me a long time to fix. There were also many warnings about dependencies between my references.
Reference Dependency Warnings
To solve these warnings I have to manually edit my .csproj file in a text editor. This MSDN article about Troubleshooting .NET Framework Targeting Errors suggests replacing references with the simple form. I tried this, but it did not seem to make a difference. I also discovered that editing the .csproj file can cause the project to not load in Visual Studio. It would tell me the line with the error (which was the line after the edit I had made) and I fixed it by copying one of the other references over the edit I had made, and then changing the reference name back to what I wanted it to be. This got rid of any characters VS was not expecting.
I then noticed that some of my references still had ‘net45’ in their paths, as in the example below. Correcting this to ‘net40’ fixed many errors, and if you look in the file structure of your project you should see that there is a 45 and a 40 folder.
<Reference Include="Microsoft.Data.OData, Version=188.8.131.52, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..packagesMicrosoft.Data.OData.5.5.0libnet40Microsoft.Data.OData.dll</HintPath> </Reference>
But I still had one big error: System.Web.Http was not behaving itself and believed it relied on another reference (I belive Newtonsoft.Json) that was .Net 4.5. I tried many things, including removing and re-adding the reference to System.Web.Http, and even removing all references to System.Web.* and re-adding them; still the same error. I also tried similar things with Newtonsoft.Json. Eventually the solution took fairly little work on my part, and here it is:
How I fixed System.Web.Http
- In visual Studio, click the Tools menu and select Library Package Manager > Manage NuGet Packages for Solution
- Click on Updates
- Update all Microsoft packages (I also updated JQuery, etc, but this shouldn’t be necessary if you don’t want to do this)
- Click Restart Now.
- Rebuild project.
My project now build without any errors 🙂
One further problem…
My project built, but when I ran it and tried to access the database I received an Entity Framework error, it had failed to load.
- Open Nuget Package Manager (Tools > Library Package Manager > Manage NuGet Packages for Solution) and locate Entity Framework in the list of installed packages.
- Click the Manage button next to Entity Framework.
- Un-tick the boxes next to your project(s) that are using Entity Framework (this will uninstall Entity Framework)
- Click on Online and use the search box in the top right to search for ‘entity’.
- Click on the Entity Framework package and then the Install button. The correct version for your .Net framework should download.
- Click the Restart Now button.
- Build and run your project to test.
Are you still having problems?
If your project still contains reference errors then you may need to use the Package Manager Console (Tools > Library Package Manager > Package Manager Console) to uninstall and reinstall the related packages. This was one of the things I tried before I remembered that the NuGet package manager would probably be easier. One problem I ran into was that you cannot uninstall a package that is relied upon by another package, so you may end up uninstalling and reinstalling a lot more packages than you really need to.
It seems that downgrading a project can be simple, but finding the solution can take a long time. If Visual Studio had a tool to help with this (especially upgrading a project) I think it would save a lot of programming hours.
Edit: I had a further problem with my application causing an error when it was deployed to the server. Here’s how I fixed it: Error when deploying a .Net 4.0 project (that was downgraded from .Net 4.5) to a .Net 4.0 server.
If you have had to downgrade a project in Visual Studio and have any more tips, tricks or links to helpful resources (or even questions) please leave a comment.