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

PPCMD 61 Reputation points
2022-09-17T11:44:11.667+00:00

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.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,133 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,373 questions
0 comments No comments
{count} votes

Accepted answer
  1. Chen Li - MSFT 1,221 Reputation points
    2022-09-20T07:45:04.813+00:00

    Hi @PPCMD ,

    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

    2 people found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,711 Reputation points
    2022-09-17T15:39:15.24+00:00

    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.

    2 people found this answer helpful.
    0 comments No comments

  2. Avadh Darji 0 Reputation points
    2024-02-09T10:58:34.8633333+00:00

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