简体   繁体   English

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

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

I got a problem two days ago while working on a project.两天前我在做一个项目时遇到了一个问题。

When I try to create a new controller or a view by hovering over the View() method and then clicking "Add View", VS generates nothing and no error message but these lines :当我尝试通过将鼠标悬停在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)

I tried to update VS to latest version which is now 16.4.3 Community edition but with no luck.我试图将 VS 更新到最新版本,现在是 16.4.3 社区版,但没有运气。

Tried to reinstall Microsoft.VisualStudio.Web.CodeGeneration from NuGet but no luck again.试图从 NuGet 重新安装Microsoft.VisualStudio.Web.CodeGeneration但再次没有运气。

Tried to delete .vs folder and no luck again.试图删除 .vs 文件夹,但再次失败。

What could be the problem?可能是什么问题呢? It was working normally before that but the scaffolding stopped working.在此之前它可以正常工作,但脚手架停止工作。

Thank you谢谢

It turns out it has something with a certain c# language syntax.事实证明,它具有某种 c# 语言语法。 Reading this issue on GitHub helped me to find a workaround.在 GitHub 上阅读此问题帮助我找到了解决方法。

What helped me fixing the problem is this comment :帮助我解决问题的是这条评论

By adding this package:通过添加这个包:

Microsoft.CodeAnalysis.CSharp.Workspaces

In the comment, the package version was 3.3.1, but I installed 3.4.0 and it works like a charm.在评论中,包版本是 3.3.1,但我安装了 3.4.0,它的工作原理很吸引人。

I had the same issue.我遇到过同样的问题。 Ubuntu 18.04.5 LTS, Rider EAP 2020.3. Ubuntu 18.04.5 LTS,Rider EAP 2020.3。 C# 9, dotnet-aspnet-codegenerator version '5.0.1', dotnet 5.0.102. 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)

In desperation mode started trying everything and ran into this and it worked with my configuration.在绝望模式下开始尝试一切并遇到这个问题,它与我的配置一起工作。

In my case I fixed it changing the target of the project to "Net 5.0" (I know this is not always posible) and after that, through (Tools/Manage Nuget Packages) updating these packages to the latest version (in this moment is 5.0.9):在我的情况下,我修复了它,将项目的目标更改为“Net 5.0”(我知道这并不总是可行的),然后通过(工具/管理 Nuget 包)将这些包更新到最新版本(此时是5.0.9):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用Entity Framework 6(非实体框架核心)开发Asp.net核心MVC应用程序吗? - Can I develop an Asp.net core MVC application using Entity Framework 6 (not Entity Framework core)? Asp.net Core 2.1-布局控制器 - Asp.net core 2.1 - Layout controller ASP.NET MVC Core和Entity Framework中的ToListAsync无法正常工作 - ToListAsync in ASP.NET MVC Core and Entity Framework not working 无法将ASP.NET MVC Core 2.0与实体框架连接 - Trouble connecting ASP.NET MVC Core 2.0 with Entity Framework Asp.Net 核心 MVC 微软实体框架 - Asp.Net core MVC Microsoft Entity Framework 脚手架后外键显示在asp.net mvc核心索引页面上的问题 - Problem with Foreign keys displaying on asp.net mvc core index pages after scaffolding 使用实体框架脚手架错误添加带有视图的新 MVC 5 Controller - Adding A New MVC 5 Controller with Views Using Entity Framework Scaffolding Error ASP.NET Core 使用 MVC 和 Entity Framework - 编辑方法不显示值 - ASP.NET Core using MVC with Entity Framework - Edit method not showing values 如何从 ASP.NET MVC Core 2.1 升级到 ASP.NET MVC Core 6? - How to upgrade from ASP.NET MVC Core 2.1 to ASP.NET MVC Core 6? 在ASP.NET MVC中使用实体框架时出现问题 - Problem when using Entity Framework in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM