如何从.net core 5迁移到.net core 8 Web api项目

问题描述 投票:0回答:2

我想将我的rest API项目从.net core 5迁移到.net core 8。请不要问为什么不从5迁移到6,客户要求这样做。

我做了什么

  • 项目值已从.net core 5更改为.net core 8

    <TargetFramework>net8.0</TargetFramework>
    
  • 项目依赖项已针对 .net core 版本和可用性进行了更新。

    <PackageReference Include="AspNetCore.Authentication.Plugin" Version="1.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4"/>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    
  • 在将startup.cs合并到program.cs时,我对如何做以及如何解决一些依赖项感到困惑(图像的左侧。我参考了基于.net core 8的新项目(右侧)图像)

任何人都可以帮助我实现此迁移吗?

c# .net-core migration asp.net-core-5.0 asp.net-core-8
2个回答
0
投票

我也做了一些同样的事情,您可以在.net 8项目中实现相同的Startup.cs文件和Program.cs文件。不需要改变任何东西。这将起作用(除了一些已弃用的功能)。 或者您可以使用 Upgrade Assistant 将 .net 5 迁移到 .net 8


0
投票
builder.Services.AddDbContext<MvcMovieContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("MvcMovieContext"))

此代码可以注册 MvcMovieContext 的数据库上下文服务并将其配置为使用 SQL Server 数据库连接字符串。 并且您可以使用以下示例代码来实现种子数据配置

using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;

    SeedData.Initialize(services);
}

此代码使用自定义用户实体类(MovieUser)和角色实体类(IdentityRole)配置身份验证服务:

builder.Services.AddIdentity<MovieUser, IdentityRole>(options =>
{
    options.SignIn.RequireConfirmedAccount = false;
})
.AddEntityFrameworkStores<MvcMovieContext>();

这是一个完整的示例,您可以用作参考:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<MvcMovieContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("MvcMovieContext")));

builder.Services.AddIdentity<MovieUser, IdentityRole>(options =>
{
    options.SignIn.RequireConfirmedAccount = false;
})
.AddEntityFrameworkStores<MvcMovieContext>();
// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;

    var dbContext = services.GetRequiredService<MvcMovieContext>();
    var userManager = services.GetRequiredService<UserManager<MovieUser>>();
    var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();

   
    SeedData.Initialize(dbContext, userManager, roleManager);
}

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
© www.soinside.com 2019 - 2024. All rights reserved.