Sunday 8 January 2023

A list of common migration errors from .Net Core 2 or .Net Core 3.1 to .Net 6 or .Net 7

You may encounter these errors when migrating your project to .Net 6 or .Net 7. Below is the error.

1.  'The ConnectionString property has not been initialized.'

In .Net 6 or 7, use has to use the keyword ConnectionStrings instead of ConnectionString otherwise you will get an error, connection string property has not been initialized.







2. A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)'


If you are getting the above error this means you need to make the connection string encrypted false in app settings. 











3. An unhandled exception occurred while processing the request. InvalidOperationException: Unable to resolve service for type 'Service.IService' while attempting to activate


In .Net 6 or 7, we don’t have a start-up file. So, you must include everything in Program.cs. file


// Register interface and classes

builder.Services.AddScoped<IHotelService, HotelService>();

 

In Program.cs file, Register dependency before builder.build() command. It will resolve your issue.











4. InvalidOperationException: Unable to resolve service for type 'Data.LocatorDBContext' while attempting to activate 'Service.HotelService'. or
System.AggregateException: 'Some services are not able to be constructed


If you are facing the above issue this means you have not resolved the context in the program.cs and have not included the connection string in the Program.cs. Add the below lines and it will resolve the dependency.

 

builder.Services.AddDbContext<LocatorDBContext>(options =>

        options.UseSqlServer(builder.Configuration.GetConnectionString("LocatorDB")));







For UseSQLserver keyword, you need to include Nuget package.


Microsoft.EntityFrameworkcore.SQLServer


5. System.InvalidOperationException: 'No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.'

This means you have not registered MVC views in Program.cs file. Just add the below line in program.cs file. 


builder.Services.AddControllersWithViews();














6. No web page was found for the web address: http://localhost:5242/hotel/index


This error will occur if you have not registered routing in the program.cs file. Include the below line and it will work.

 

app.MapControllerRoute(

    name: "default",

    pattern: "{controller=Hotel}/{action=Index}/{id?}");












This will resolve the issue. I hope you like the article.

No comments:

Post a Comment

How to find the reason of HTTP Error 500.30 - ASP.NET Core app failed to start in Azure App Service

HTTP Error 500.30 - The ASP.NET Core app failed to start If your web app is throwing an error HTTP error 500.30 then how to find the root ca...