The upgrade from previous versions of .NET to .NET 6 can cause both build errors and runtime errors. The build errors can be easy to fix because you are notified and prevented from building the projects. The runtime errors, on the other hand are difficult to debug, unless you have full system tests already integrated.
Therefore, I would recommend making sure if you have tests that they all are passing before you start the upgrade process. This will ensure that after the upgrade is complete, you can run the tests and if all the tests pass then the upgrade was successful.
Where to start?
The best place to start is with your test projects (Unit Tests, Integration Tests, Acceptance Tests). Since these projects are responsible for confirming that your solution is working as intended its best to upgrade them first one by one and make sure they are still passing before moving onto other projects in your solution.
Preforming the upgrade
Following are the steps that I followed for each project to upgrade the .NET 6. I will also include a list of problems that I ran into in each step and the solutions that I found to fix them.
Step 1 – Upgrading .NET Target Framework
- Open the .csproj file by double clicking the project in Visual Studio. At the top of the file you will see
- Replace (Your current Framework) with
- Next, save and build – I would recommend to clean and rebuild the solution every time you make a change. This might fix some easy problems.
NOTE: Most build errors that I ran into where outdated packages or version conflicts against other projects. The error messages should have which project or which package needs to be ungraded. Fix the conflicts and rebuild. - Finally, run all tests, make sure they all pass
Potential Problems
- Problem (Runtime): I upgraded two separate UnitTest Projects and ran into this same error when trying to run the tests. The runtime error below is what exception I was receiving.
- Solution: The below code block from the .csproj file (CopyLocalLockFileAssemblies) was causing this error. By setting it to TRUE did not fix the problem I had to remove the code block entirely.
- Problem (Runtime): This problem happened twice also, but this time it was when upgrading an integration test project. This happened because I did not have Entityframework as a package.
System.TypeInitializationException : The type initializer for 'Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor' threw an exception.
---- System.TypeInitializationException : The type initializer for 'Microsoft.EntityFrameworkCore.Query.QueryableMethods' threw an exception.
- Solution: To fix this error I had to add the following packages to the project
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
Step 2 – Upgrading NuGet Packages
In Visual Studio right click on the project and navigate to Manage NuGet Packages. Under updates you will see a list of packages that need to be updated.
Note: I would recommend updating every package one by one Build and Test make sure the package does not break anything. This will help you debug if something does break.
- Update the packages to the latest version. Some code changes and references might need to be fixed.
- Some packages also might require additional packages once they are upgraded.
Note: Some packages might not be compatible to .NET 6. So, if they have an update but are breaking and you cannot find a solution it might be easier to revert the update and wait for a new release to update that package.
Next Steps When Moving from .NET CORE to .NET 6
Repeat the above steps on each project until the entire solution is updated.
To conclude, here are some recommendations I have to make the process easier and to have less problems.
- Make sure your solution is using some sort of version control. Commit after each project is updated to keep from losing work.
- If a project requires another project to have the new version, then update that project as little as you can to get the first one to run. Then once you get one done you can move back to the other. This will allow you to debug issues a lot easier.
- Every time you make a change Clean and Rebuild the solution and run any test suites. This will allow you to pinpoint what caused and error to easily fix it.