Migration From .NET Core 3.1 To .NET 6

Introduction

.NET 6 is an LTS (Long Term Support) Version. It will be supported for three years. It is the latest long-term support release. The previous version, .NET Core 3.1 support, was finalized in December 2022, and support for .NET 5 already ended in May 2022. This article describes how to upgrade the solution from .NET Core 3.1 to .NET 6.0 with an example of a web application project.

Pre-requisites

  • Visual Studio 2022

Step 1. Change the Project Target Framework

Open your current project in Visual Studio 2022. Right-click on the Project name in Solution Explorer and select Properties.

Project Target Framework

Then select the target framework to .NET 6.0 as shown below and save it.

Select Framework

After this step, the base framework changed. One can check or alternatively change the framework from the project_name.csproj file. The code looks similar to the code depicted below.

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
    </PropertyGroup>
</Project>

Step 2. Updating Package references

While migrating from ASP.NET Core 3.1 to .NET 6.0, it is very important to upgrade the NuGet packages used in the current project.

Method 1 for upgrading Package

Using NuGet Package Manager, you can change the package version to a compatible version with .NET 6. Please see the snap below for reference.

Migration From .NET Core 3.1 to .NET 6

Method 2 for upgrading Package

Go to Project.csproj file and upgrade packages as shown below. For instance, upgrade the Microsoft package.AspNetCore.JsonPatch and Microsoft.EntityFrameworkCore.Tools and so on from version 3.1.6 to 6.0.0, as illustrated below.

<ItemGroup>
    - <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.1.6" />
    - <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.6" />
    - <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.6" />
    - <PackageReference Include="System.Net.Http.Json" Version="3.2.1" />
    + <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="6.0.0" />
    + <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0" />
    + <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
    + <PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
</ItemGroup>

Step 3. Delete Obj and bin folder

You may need to delete the existing bin and obj folder. Go to the respective project directory and delete those folders. Additionally, you can delete the NuGet cache by running the below command.

dotnet nuget locals --clear all

Step 4. Build the solution

After Step 3, build the solution and see whether there are errors or if your app is built successfully. If there are errors based on an error message, correct the code and rebuild the solution. On successful build of the application, your app is upgraded to .NET 6.0.

The above three steps are required to follow to upgrade the web application to migrate from .NET Core 3.1 to .NET 6.0.

Following are some changes you need to consider for upgrading the ASP.NET Core 3.1 web application.NET6.

Minimal hosting and startup.cs file changes

Note that minimal hosting unifies the Startup.cs and Program.cs to a single Program.cs file. Moreover, the ConfigureServices and Configure are no longer used in .NET 6.0. In .NET 6, there is no startup.cs file and the code snippet found in startup.cs is merged in Program.cs file. So, if you create a new .NET 6 project in Visual Studio 2022, then you may see only Program.cs file, but in the up-gradation case, it retains startup.cs file.

Model Binding

Datetime values are model bound as UTC time zone. For applications built on ASP .NET Core 5 and later, model binding binds the DateTime as UTC time zone whereas, in applications built using ASP.NET Core 3.1 and earlier, Datetime values were model bound as local time, and the time zone was determined by the server.

Docker Image

If your app uses docker, then you need to pull a Docker image that consists of ASP.NET Core 6.0 runtime as well. The below command can be used for that.

docker pull mcr.microsoft.com/dotnet/aspnet:6.0

Summary

We have migrated a very basic .NET Core 3.1-based project to the .NET 6 version. Migration tasks depend on how many services, features, and libraries you have used in your older version.

The more libraries and features, the more up-gradation or migration steps.

To demonstrate this article, I have used a very basic CRUD operation with Entity Framework, which I did very easily.

Note. Migration from ASP.NET Core 3.1 to .NET 6.0 may vary from project to project.

References

Thanks for the reading!