简体   繁体   中英

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 :

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.

Tried to reinstall Microsoft.VisualStudio.Web.CodeGeneration from NuGet but no luck again.

Tried to delete .vs folder and no luck again.

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. Reading this issue on GitHub helped me to find a workaround.

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.

I had the same issue. Ubuntu 18.04.5 LTS, Rider EAP 2020.3. C# 9, dotnet-aspnet-codegenerator version '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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM