8

 scaffolding was not worked The package restore failed. Rolling back package changes in VS2019 for asp.net core. When,I try to add API Controller using EF. This didn't work for me -Try to Clear All Nuget Caches , -Try clearing the ComponentModelCache Can't Add View from Controller in VS 2015 : "There was an error running the selected code generator" It does not solve this error.

can anyone help me?

jps
  • 20,041
  • 15
  • 75
  • 79
Risk
  • 103
  • 1
  • 1
  • 8

8 Answers8

7

I had the same issue while adding the view for an IActionResult in .net 5.0 MVC using VS2019, before updating all the installed packages.

For me, restarting the Visual Studio works.

After restart I tried again, auto scaffolding installed this package "Microsoft.VisualStudio.Web.CodeGeneration.Design" along with generating a View.

Umair Ijaz
  • 191
  • 1
  • 7
4

There are a couple of reasons that might account for this behaviour. Some of the reasons, as I have experienced on different occasions, include but might not be limited to the following:

  1. Outdated Visual Studio Environment: In this case, you might attempt to update your version of VS.

  2. Outdated Packages: In this case, update some or all of the packages in your project. To do this, go to Tools -> Nuget Package Manager -> Manage Nuget Package for Solution. Choose the packages that you want to update. if you are not sure, check Select all packages

  • Updating all Nuget packages worked for me (VS2022). Since I'd been using .net 6 and this pushed the packages into .net 7, I had to also update the projects' target framework to net7.0. – MJ Hughes Jan 26 '23 at 14:32
3

For EF Core packages version 5.0.5 I was only able to solve this using the command line tool. See answer by johanjuulj here Visual Studio error: There was an error running the selected code generator

dotnet aspnet-codegenerator controller -m MyModel -dc MyDbContext -name MyModelssController -async
Nick Pearce
  • 720
  • 5
  • 14
  • 1
    This worked for me, I tried many solutions, Clearing folder, updating vs, update packages, restarting vs, etc. Thanks – Sayyed Dawood Apr 27 '21 at 22:35
3

1 - Go to Tools

2 - Options

3- NuGet Package Manager

4- General

5 - Clear All NuGet Cache(s)

0

In my case I needed to create an API controller and the only solution that worked for me was to download from the visual studio installer, in the individual features section: all the features of the SKD.NET framework, all the .NET Framework support packages, ASP.NET and web development tools, and a few SQL features that I need.

0

I tried some of the suggestions above- but in my case I had all the versions of EF aligned on version 5.0.9 except for the autogenerated Swashbuckle.

I removed the following:

<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGenerators.Mvc" Version="6.0.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />

then I had to reinstall (because it is used by my code):

<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.9" />

Finally I ran the View scaffold again and now it worked!

I can see that during scaffolding the following package was auto installed:

<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />
4b0
  • 21,981
  • 30
  • 95
  • 142
GBCode
  • 1
0

Similar issue in VS 2022. Trying to add an API controller to a separate ASP.Net Core Web API project in a solution with a separate project containing the POCO and DBContext. Tried some of the same fixes above; no luck.

I added an Api folder to the project containing the POCO and DBContext and added an API controller there with no problem. Then I copied the controller class to the Controller folder in the API project. I fixed the Route attribute in the controller, removing the "api/" part, added a connection string to the appsettings.json and called builder.Services.AddDbContext... in the API project's Program.cs.

This worked for me.

Bill Needels
  • 505
  • 4
  • 13
0

I had the same problem and tried many of the solutions but they didn't work. My project used .Net Core 3.1 and what fixed the issue was updating all NuGet packages in my project to the latest version of 3.1.x

Hope this helps.

sidereal
  • 25
  • 6