'Error: "There was an error running the selected code generator: Package restore failed"

I am trying to add controller to my solution in ASP.NET Core project:

When I try to do so I get this error:

I get the same message for adding minimal dependencies and full dependencies for controller.



Solution 1:[1]

I also had this issue. "Add Controller>API Controller with actions, using Entity Framework" would give the "Package Restore Failed" error.

As Anish stated, it seems to be due to package versions being mis-aligned. I was able to resolve this issue using "Manage NUGET Packages for Solution", then performing an "Update All". This set my AspNetCore version to 2.1.5 and resolved my "Package Restore Failed" error, but then led to another error, "NETCore version 2.1.5 not found". Apparently the scaffolding code generator needs the AspNetCore and the NETCore versions to be in sync, so I manually downloaded and installed the NETCore version 2.1.5 from Microsoft Downloads. This worked, and I was finally able to generate Controllers.

Solution 2:[2]

I was getting the same error while making a new controller. I've fixed it like this. Actually, VS only had the Offline Package source and could not resolve the packages needed.

Add the online reference: Tools > nuget package manager > package manager settings > Package Sources

Add source: https://api.nuget.org/v3/index.json

Solution 3:[3]

If no answer works for you, try running the code generator from the command line.

For my sln with multiple projects, with net 5 and some NuGet packages of 5.0.5 and some of 5.0.2, Only code generator through the command line worked. Make Sure it is installed.

or install it by the following command

dotnet tool install -g dotnet-aspnet-codegenerator

or update it by the following command

dotnet tool update -g dotnet-aspnet-codegenerator

The basic code generator commands can be found here

Some of them are:

Generator           Operation
area                Scaffolds an Area
controller          Scaffolds a controller
identity            Scaffolds Identity
razorpage           Scaffolds Razor Pages
view                Scaffolds a view

For example:

dotnet-aspnet-codegenerator identity --dbContext MyDbContextClass

To get help:

dotnet-aspnet-codegenerator [YourGenerator] -h

Solution 4:[4]

  1. VS2019 [5.0].

  2. Update NuGet Packages (Tools -> Nuget Package Manager -> Manage NuGet packages for solution -> Click on the Updates tab, select all and run update.

  3. Solution -> Clean

  4. solution -> Build

  5. create a Controller.

I try everything but the above method work for me

Solution 5:[5]

I encountered this issue with net5.0, specifically against version 5.0.5 of some dependencies. I downgraded my nuget packages from 5.0.5 to 5.0.4 for these:

"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.4"
"Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.4"
"Microsoft.AspNetCore.Identity.UI" Version="5.0.4" 
"Microsoft.EntityFrameworkCore.Tools" Version="5.0.4"

Solution 6:[6]

I just recently ran into the same issue.

I resolved it by eventually taking a look at each individual .csproj files included in my solution and fixing all the versions of the microsoft libraries that were included.

I changed the metapackage that i was referencing from "Microsoft.AspNetCore.All" to "Microsoft.AspNetCore.App", i then loaded up the reference list on nuget for the "App" package and removed any references to libraries that are already included in the metapackage.

I then made sure that i fixed the versions of any outstanding packages to match the version of the metapackage that the project automatically chooses ie in my case 2.2.0.

Something that tripped me up was that if you have multiple projects included in your solution you need to make sure that they reference the same metapackage as if there is a version mismatch between projects included in your solution you will get this issue too.

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.5" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
  </ItemGroup>

Changed to this.

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
  </ItemGroup>

Solution 7:[7]

I just had this problem whilst adding a controller to a Core API with Entity Framework project. I'm using VS 16.8.5 with the most recent EF core, version 5.03. The class containing my DBContext class referenced EF 5.03 .

I (eventually!) noticed whilst browsing Nuget that the various code generation packages (none of which were referenced in my .csproj file, I think because ASP.Net core ships as a framework since 3.0 but correct me if I am wrong someone!), and in particular Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCode were 5.02. I didn't touch my ASP.Net project, instead I dowgraded the other EF projects to 5.02 and it solved the problem.

Solution 8:[8]

Just update the NUGET packages from the Nuget Package Manager.

Solution 9:[9]

i have same error. and Update NuGet Packages (Tools -> Nuget Package Manager -> Manage NuGet packages for solution -> Click on installed. You need select Version with notice Dependencies.

Example my option is fine with:

Microsoft.AspNetCore.Identity.EntityFramework Core with version 3.1.12

Microsoft.EntityFrameworkCore.Tools with version 3.1.12

Microsoft.EntityFrameworkCore.SqlServer with version 3.1.12

Microsoft.VisualStudio.Web.CodeGeneration.Design with version 3.1.5

Solution 10:[10]

I had the problem with a blazor server application version 5.0.5 and Microsoft Identity scaffolding. The highest available version of the CodeGeneration.Design package was 5.0.2, so i downgraded the other Microsoft packages (specially EntityFramework) to 5.0.2 and it solved the problem.

Solution 11:[11]

I am also facing this issue.

Please follow this step,

  • Clean Your Solution
  • Open Nuget Manager
  • Check this version Microsoft.EntityFrameworkCore (I am using 5.0.8)
  • Check this version Microsoft.EntityFrameworkCore.Design (I am using 5.0.8)
  • Check this version Microsoft.EntityFrameworkCore.Tools (I am using 5.0.8)
  • Check this version Microsoft.EntityFrameworkCore.SqlServer (I am using 5.0.8)
  • Check this version Microsoft.VisualStudio.Web.CodeGeneration.Design (I am using 5.0.2)

Check this image also

  • After that Rebuild your solution and Create Scaffolding Controller

Solution 12:[12]

Had exactly same problem, in my situation CodeGenerator was missing

I have added this item into ItemGroup in .csproj

<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />

Solution 13:[13]

What fixed it for me after I couldn't scaffold IdentityFramework was by

  1. Checking VS2019 for updates.
  2. Update NuGet Packages (Tools -> Nuget Package Manager -> Manage NuGet packages for solution -> Click on the updates tab, select all and run update.
  3. Retry scaffolding identity

Solution 14:[14]

I had resolved it by update two files

Microsoft.VisualStudio.Web.CodeGeneration and Microsoft.VisualStudio.Web.CodeGeneration.Design

. Its version should be match with other packages version in application.

Solution 15:[15]

Cleaning the solution showed me an error of NuGet packages needed to be updated! I updated them and build the solution. Build Successful and I was able to create the controller class.

Solution 16:[16]

Trying to add MVC controller with views using EF for MVC project using Net5.0.

Using the following NuGet packages specific versions worked for me, while the problem was solved by using less version than the version of Microsoft.EntityFrameworkCore, for both Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Tools, these packages are referenced in the MVC project.

The correct packages are:

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6">
           <PrivateAssets>all</PrivateAssets>
           <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.7" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />   
</ItemGroup>

Solution 17:[17]

I also faced this same error when i was trying to scaffold identity template. I resolve this issue by updating nuget packages of the two major project of concerns(I mean the the two projects that has something to do with what i was to implement).

Solution 18:[18]

The problem is that you have some of the older versions of nuget pacakges installed in your project and when you try to scaffold the Asp.net core tries to install the latest packages which are required for scaffolding at this point Asp.net core throws such exception.

I suggest you to update your nuget packages and than try scafolding.

Solution 19:[19]

I also faced the same issue, Here is How I solved the Issue

There was an error running the selected code generation, 'Package restore failed. Rolling back package canges for web'

1- Check if your Solution has multiple projects, please check their Target Dot.net Framework.(in my case it was .Net Standard 1.6 for class libraries & .NetCoreApp 1.0 for Web Project, I changed it to .NetCoreApp 1.1)

2- After having the same framework, clean the web project, Rebuilt and Add new Controller.

If its successful fine otherwise You might encounter another error e.g

'There was an error running the code generator: 'No executable found matching command "dotnet-aspnet-codegenerator"'

If you have project.json file open it other wise open .csproj.user project in note pad, please add following Please note based on your .net version you might have different version no.

You may find instruction in ScaffoldingReadMe.txt file if its generated in your project

Solution 20:[20]

All I had to do was open the properties of my web project and change the TargetFramework from 2.1 to 2.2. Or to match whatever version of the framework your business and object layer are using.

Solution 21:[21]

Had the same problem, but updating all the NuGet Packages has solved the problem. Right click on <your project name> file -> Manage NuGet Packages -> Updates -> Select all packages -> Update

Solution 22:[22]

I'm running .NET Core (and Entity Framework Core) 3.1.x.

I got this exact error and tried updating all the nuget packages and other relevant solutions already mentioned in the answers here.

The issue was simply that my database server was not running (it runs on a local VM). In other words, my database context (i.e. ApplicationDbContext) mentioned in the 'Add Controller...' window, was not able to access the db. Once I started the db server, my scaffolding created without issue.

Keep also in mind, the model/class (i.e. table) that the controller and views were referencing had not been created yet (I hadn't run add-migration yet). So, it just needed the db connection only.

It's kind of a silly (obvious?) solution, but very misleading when looking at the 'Package Restore Failed' error message.

Solution 23:[23]

I had a similar issue with entity framework core sqlite nuget packages. I installed sqlite and sqlite core packages fixed this. Probably a needy package is missing. Also make sure SQL Server and Server Agent are running. Check those on SQL Server Configuration > SQL Server Services > Right click on SQL Server or Server Agent and start the service then restart the server. Guess this might help someone

Solution 24:[24]

My solution -Vs2019 all nuget packages upgrade

Solution 25:[25]

As someone mentioned earlier, I updated all NuGet packages from Tools->Nuget Package Manager -> Manage NuGet Packages for Solution -> Updates tab -> Update All. I was then able to add a controller with EF and have VS generate the associated views.

Solution 26:[26]

Just update all Nuget Packages , Clean Solution and the rebuild solution. It solve the issues for me.

Solution 27:[27]

I just updated EntityFrameworkCore from Version 3.1.10 to 3.1.13 and it solved the problem. My Project file looks like:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.13" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.13" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.13" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.13">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" />
  </ItemGroup>

</Project>

Solution 28:[28]

I had this same issue when creating a new 'Identity' scaffolded item. I managed to get this working by removing everything within the <ItemGroup> tags within the csproj file and running the code generator. The generator then installs packages that it needs.

Solution 29:[29]

In case you are using .net 5.0.14, downgrading every packages with version 5.0.14 from 5.0.14 to 5.0.12 fixed the problem for me.