'there was an error running the selected code generator in .net6

I use .net 6 and when I use Add an ASP.NET Core MVC controller with views, using Entity Framework Core(I do this in Area). I get an error.

I searched a lot but did not find any

error:

enter image description here

DBContext:


namespace PotLearn.DataLayer.Context
{
    public class PotLearnContext:DbContext
    {
        public PotLearnContext(DbContextOptions<PotLearnContext> options)
    : base(options)
        {
        }
        #region User
        public DbSet<User> Users { get; set; }
        #endregion

    }
}

program.cs:


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
#region DataBase Context
    var connectionString = builder.Configuration.GetConnectionString("PotLearnConnection");
    builder.Services.AddDbContext<PotLearnContext>(options => options.UseSqlServer(connectionString));
#endregion
var app = builder.Build();

// 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.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
      name: "areas",
      pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
    );
});

app.Run();

How can I fix this error?

Thankyou.



Solution 1:[1]

This is issue seems to be version issue. Your .NET 6.0 is not matching with the version with Web.CodeGeneration.

SOLUTION - 1: Go to NuGet package Manager (Tools > NuGet Package Manager > Manage NuGet packages for solution). Then go to Updates and update the packages showing in it.

SOLUTION - 2: Clean and Rebuild your whole Solution, NOT just the project.

SOLUTION - 3:- If SOLUTION - 1 is not working then go to Browse tab and then search for Microsoft.VisualStudio.Web.CodeGeneration and based on your .NET project, install the version by selecting the version number.

Hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 noobprogrammer