there was an error running the selected code generator in .net6
Asked Answered
H

3

6

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.

Halfback answered 5/4, 2022 at 20:21 Comment(2)
If you try to clean and rebuild,will it be solved?Runty
No unfortunately it was not resolvedHalfback
K
7

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.

Koball answered 7/4, 2022 at 6:3 Comment(9)
It does not work:(Halfback
Check your versions of "Microsoft.VisualStudio.Web.CodeGeneration.Utils" and "Microsoft.VisualStudio.Web.CodeGeneration.Design". If you have lower versions, update it to the latest one. If you don't have it, then install it.Koball
I did this but it did not workHalfback
@sina, can you check your 'PotLearnContext' class. Does it have base constructor overridden?Koball
yes it has a overridden constructorHalfback
@Halfback I noticed that you wrote your DbContext in Program.cs instead of Startup.cs. Please try to write your Dbcontext dependency in Startup.cs file under ConfigureServices(). The line starting from UseHttpsRedirection() until the end should be in Configure().Koball
But There isn't Startup.cs in .net 6Halfback
Exactly the same problem here, love to hear what would be the solution.Lapp
@Halfback and @Lapp in your Program.cs can you please try this: builder.Services.AddDbContext<PotLearnContext>(options => options.UseSqlServer(connectionString).ConnectionString;) Notice, I have added ConnectionString at the end of the code.Koball
S
2

After trying to fix this issue for a long time in .Net 6, when I was trying to install "Microsoft.CodeAnalysis.CSharp.Workspaces" which happen to read in the console:

NU1107: Version conflict detected for Microsoft.CodeAnalysis.Common. project project.xxx to resolve this issue.
project.xxx -> Microsoft.CodeAnalysis.CSharp.Workspaces 4.2.0 -> Microsoft.CodeAnalysis.Common (= 4.2.0) Microsoft.VisualStudio.Web.CodeGeneration.Utils 6.0.7 -> Microsoft.DotNet.Scaffolding.Shared 6.0.7

My problem was solved after replacing Microsoft.VisualStudio.Web.CodeGeneration.Utils with Microsoft.CodeAnalysis.Common

And Microsoft.CodeAnalysis.CSharp.Workspaces with Microsoft.DotNet.Scaffolding.Shared

Schappe answered 24/7, 2022 at 20:41 Comment(1)
I can't fix using this codeFreemason
M
0

The code generator seems to fail when dealing with context class defined outside the project where code generation has been initiated.

After thorough investigation I found out that the problem is not related to any version of Code Generators or any associated packages. The problem stems from having the Context class defined in separate assembly (let's say your DataLayer project) from the one (let's say Web application project) you're using to generate your codes. I tried to let Visual Studio create a context (for my target data model) for me (which it did and saved it in the Data folder of Web application project) and the code generation worked using that context.

I examined the generated context class and compared with mine, there was not significant difference other than of-course the namespaces. So I deleted the generated context file. I copied similar file from the DataLayer project to Web application project; I edited the namespace and resolved references. I repeat the process with that edited class from DataLayer project and it run to completion without error and generated the codes but if the class is defined in external assembly (or project) it fails again. I tried moving the file out of Data folder to see if code generator is tied to it, I found it out it does not depend of Data folder.

So I concluded that the generation only worked as long as the context class is defined in the same assembly as the one initiating the code generation.

Marsland answered 2/5 at 19:53 Comment(1)
Your enter key on your keyboard seems to be failing/missing ?Damal

© 2022 - 2024 — McMap. All rights reserved.