【问题标题】:Problem scaffolding new controller using Entity Framework Core in ASP.NET Core 2.1 MVC在 ASP.NET Core 2.1 MVC 中使用 Entity Framework Core 构建新控制器时出现问题
【发布时间】:2020-01-25 17:31:35
【问题描述】:

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

当我尝试通过将鼠标悬停在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 文件夹,但再次失败。

可能是什么问题?在此之前它工作正常,但脚手架停止工作。

谢谢

【问题讨论】:

  • 首先,您需要验证您的问题是特定于 Visual Studio 还是您机器上的问题。例如,下载 Rider 并使用该工具进行尝试。 jetbrains.com/rider/download/#section=windows 。很可能是这样,但您需要确定。您也可以改用 Visual Studio 代码; code.visualstudio.com/docs/languages/dotnet 。如果是 VS 特定的,您可以在安装程序中使用 VS Repair 修复 VS。 docs.microsoft.com/en-us/visualstudio/install/… 。祝你好运。
  • @Daan 谢谢,但是这些选项中的任何一个都可以选择使用 EF 创建控制器?像 Add => Controller => Controller 使用 EF 来自动生成带有动作和视图的控制器?因为我可以轻松添加空控制器和空视图,但我的问题是使用 EF 添加新控制器
  • 不幸的是,修复VS并没有解决问题。
  • 是的,您也可以在 Rider 中生成控制器:stackoverflow.com/a/57000703/1987258。此外,VS Code 有一个扩展来帮助你。 marketplace.visualstudio.com/…
  • 我可以生成一个控制器,但不幸的是,这两种选择都没有 EF。 Rider 生成一个空控制器。我的问题是使用 EF 生成它们以生成带有操作名称和视图的控制器,我稍后可以修改它们。但是现在,我有很多模型需要为其生成代码。而且没有时间编写所有控制器并从头开始查看它。这里还有其他选择吗?

标签: asp.net-mvc asp.net-core visual-studio-2019 scaffolding


【解决方案1】:

事实证明,它具有某种 c# 语言语法。 在 GitHub 上阅读此 issue 帮助我找到了解决方法。

帮助我解决问题的是comment

通过添加这个包:

Microsoft.CodeAnalysis.CSharp.Workspaces

在评论中,软件包版本是 3.3.1,但我安装了 3.4.0,它就像一个魅力。

【讨论】:

  • 我遇到了同样的问题,但 .NET core 3.1 和 web API 应用程序。您的解决方案非常有效。
  • 感谢@Kaj 的帮助,我在 .net 5 上遇到了类似的问题,使用此方法已解决
【解决方案2】:

我有同样的问题。 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,它适用于我的配置。

【讨论】:

    【解决方案3】:

    在我的情况下,我修复了它将项目的目标更改为“Net 5.0”(我知道这并不总是可行),然后通过(工具/管理 Nuget 包)将这些包更新到最新版本(在现在是 5.0.9):

    • Microsoft.EntityFrameworkCore
    • Microsoft.EntityFrameworkCore.SqlServer
    • Microsoft.EntityFrameworkCore.Tools

    【讨论】:

      猜你喜欢
      • 2019-04-13
      • 1970-01-01
      • 2019-02-23
      • 2021-01-03
      • 2018-09-29
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多