在 ASP.NET Core 2.1 MVC 中使用 Entity Framework Core 搭建新控制器时出现问题

Problem scaffolding new controller using Entity Framework Core in ASP.NET Core 2.1 MVC

两天前我在做一个项目时遇到了问题。

当我尝试通过将鼠标悬停在 View() 方法上然后单击 "Add View" 来创建新的控制器或视图时,VS 没有生成任何内容,也没有生成任何错误消息,但这些行:

Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to compile the application in memory.
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

我尝试将 VS 更新到最新版本,现在是 16.4.3 社区版,但没有成功。

尝试从 NuGet 重新安装 Microsoft.VisualStudio.Web.CodeGeneration,但再次失败。

尝试删除 .vs 文件夹,但再次失败。

可能是什么问题?之前还正常,脚手架就不行了

谢谢

原来它有一些带有某种c#语言语法的东西。 在 GitHub 上阅读此 issue 帮助我找到了解决方法。

帮助我解决问题的是 comment:

通过添加这个包:

Microsoft.CodeAnalysis.CSharp.Workspaces

在评论中,包版本是3.3.1,但我安装了3.4.0,效果很好。

我遇到了同样的问题。 Ubuntu 18.04.5 LTS,骑士 EAP 2020.3。 C# 9,dotnet-aspnet-codegenerator 版本“5.0.1”,dotnet 5.0.102。

Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to compile the application in memory.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Person'
A file matching the name MvcControllerWithContext.cshtml was not found within any of the folders: 
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

在绝望模式下开始尝试一切,运行 into this 它与我的配置一起工作。

在我的例子中,我修复了它,将项目的目标更改为“Net 5.0”(我知道这并不总是可行的),然后通过(Tools/Manage Nuget 包)将这些包更新到最新版本(此时为5.0.9):

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.工具