在 .net6 中运行所选代码生成器时出错

sin*_*ina 6 c# asp.net-core .net-6.0

我使用 .net 6,当我使用添加带有视图的 ASP.NET Core MVC 控制器时,使用 Entity Framework Core(我在区域中执行此操作)。我收到一个错误。

我搜索了很多但没有找到

错误:

在此输入图像描述

数据库上下文:


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

    }
}
Run Code Online (Sandbox Code Playgroud)

程序.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();

Run Code Online (Sandbox Code Playgroud)

我该如何修复这个错误?

谢谢。

noo*_*mer 7

这个问题似乎是版本问题。您的.NET 6.0与Web.CodeGeneration的版本不匹配。

解决方案 - 1: 转到 NuGet 包管理器(工具 > NuGet 包管理器 > 管理解决方案的 NuGet 包)。然后转到更新并更新其中显示的软件包。

解决方案 - 2: 清理并重建整个解决方案,而不仅仅是项目。

解决方案 - 3:- 如果解决方案 - 1不起作用,请转到“浏览”选项卡,然后搜索 Microsoft.VisualStudio.Web.CodeGeneration,并根据您的 .NET 项目,通过选择版本号来安装版本。

希望这可以帮助。