question

PPCMD-9649 avatar image
0 Votes"
PPCMD-9649 asked ChenLi-MSFT answered

Downgrade ASP.NET Core app from .NET 6.0 to .NET Core 3.1

I have made an ASP.NET Core web application, using .NET 6.0. However, when I obtained a hosting server, I learnt that my hosting provider has installed .NET 5.0. As a result, I now have to downgrade my project from .NET 6.0 to a previous version. However, I am facing issues while doing so. When I try to build the project, I run into errors, due to which I have to change syntaxes in system-generated files, such as Program.cs and Startup.cs. Then, I also face trouble updating the NuGet packages and once done, I run into errors while trying to run the Update-Database command. I would like to know the required steps to downgrade my project, in a slightly detailed manner, since I am an amateur in using .NET Core. My project is a Razor pages project and has Identity installed, with a few modifications, such as additional fields. How should I proceed? Thanks in advance.

dotnet-aspnet-core-generaldotnet-aspnet-core-blazor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
2 Votes"
Bruce-SqlWork answered Bruce-SqlWork edited

First net 5 is out of support (ended 5/10/2022) and 3.1 support ends in December. I’d question you host about this.

The easiest approach is to recreate your project in net 5. Then add packages. Use the program file, startup and project files. Or add your old code to the new project.

Note: EF has changed more than razor pages. You will probably need to start with a fresh database, and may need code changes.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ChenLi-MSFT avatar image
1 Vote"
ChenLi-MSFT answered

Hi @PPCMD-9649 ,

I created a new ASP.NET Core web application using version 6.0 and Identity, and successfully downgraded to version 3.1. You can refer to my steps:

1. Change the .csproj file:

 <PropertyGroup>
     <TargetFramework>netcoreapp3.1</TargetFramework>
     <Nullable>enable</Nullable>
     <ImplicitUsings>disable</ImplicitUsings>
 </PropertyGroup>

Note: <ImplicitUsings> must be set to disable, otherwise the operation will automatically generate the GlobalUsings.g.cs file resulting in errors.

2. Update Startup.cs and Program.cs files according to .net core 3.1, for example:
Startup.cs:

 public class Startup
     {
         public Startup(IConfiguration configuration)
         {
             Configuration = configuration;
         }
    
         public IConfiguration Configuration { get; }
    
         // This method gets called by the runtime. Use this method to add services to the container.
         public void ConfigureServices(IServiceCollection services)
         {
             services.AddDbContext<ApplicationDbContext>(options =>
                 options.UseSqlServer(
                     Configuration.GetConnectionString("DefaultConnection")));
             services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                 .AddEntityFrameworkStores<ApplicationDbContext>();
             services.AddRazorPages();
         }
    
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
         {
             if (env.IsDevelopment())
             {
                 app.UseDeveloperExceptionPage();
                 app.UseDatabaseErrorPage();
             }
             else
             {
                 app.UseExceptionHandler("/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.UseAuthentication();
             app.UseAuthorization();
    
             app.UseEndpoints(endpoints =>
             {
                 endpoints.MapRazorPages();
             });
         }
     }

Program.cs:

 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>();
                 });
     }

3. Downgrade installed Nuget packages:
Downgrade Nuget packages need to pay attention to the order, otherwise the installation may fail. So I suggest you to uninstall the Nuget package and then install the package suitable for version 3.1.
Your Nuget package will be displayed in the .csproj file for easy inspection:
242873-image.png

4. Add explicit references to assemblies that are missing references in the code

5. Run your program
If there is a database error when registering or logging in, click the Apply Migration button on the error page or run the Update-Database command can solve this problem.


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
Chen Li



image.png (26.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.