.Net Core2.2 upgrade to 3.1 small note

Original: .Net upgrade to 3.1 Core2.2 small note

.NET Core 3.1 long-term support as a LTS version, will provide three years of support (next year on the .net5), worth the upgrade (it).

Most of the current mainstream third-party packages have provided support, 2.x => 3.1 or change is not particularly large, EF Core hole big slightly, cautious.

 

ASP.NET Core 3.1's new features

https://docs.microsoft.com/zh-cn/aspnet/core/release-notes/aspnetcore-3.1?view=aspnetcore-3.1

EF Core 3.0 major changes

https://docs.microsoft.com/zh-cn/ef/core/what-is-new/ef-core-3.0/breaking-changes

 

1, the VS2019 update to 16.4.x, will automatically install the SDK 3.1.

 

2, the upgrade project target framework to 3.1, right-project - Properties - target framework, or modify csproj file.

?
1
2
3
<PropertyGroup>
   <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

 

3, WEB project Program.cs file, WebHost instead Host

Copy the code
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
Copy the code

 

4, WEB project Startup.cs

ConfigureServices method services.AddMvc () to services.AddControllersWithViews ()

//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddControllersWithViews();

Configure method app.UseMvc () to app.UseRouting () and app.UseEndpoints ()

Copy the code
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();
    app.UseAuthorization();

    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}
Copy the code

 

5, update all (relevant) nuget to the latest package

 

6, Rebuild Solution, an error The project .... must provide a value for Configuration.

Microsoft.AspNetCore ... PackageReference removed from the Web project, as it has been included in the framework of the objectives of the 3.1.

<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />

 

It's done , at least by the compiler, currently running only encountered a pit.

A tracking query projects owned entity without corresponding owner in result.Owned entities cannot be tracked without their owner. Either include the owner entity in the result or make query non-tracking using AsNoTracking().

AutoMapper of ProjectTo <> lead, temporarily added a AsNoTracking () to solve the problem, according to the error message point of view there is another solution.

Copy the code
public IEnumerable<UserDto> GetAll()
{
    return _userRepository.GetAll(tracking: false)
        .OrderByDescending(s => s.Id)
        .ProjectTo<UserDto>(_mapper.ConfigurationProvider)
        .AsEnumerable();
}
Copy the code

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12070764.html