问题
I am trying to add controller to my solution in Asp.net Core project.
While doing so i am getting this error.
I am getting the same message for adding minimal dependencies and full dependencies for controller.
Please help me with this issue. Thanks in advance.
回答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.
回答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
回答3:
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>
回答4:
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" />
回答5:
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.
回答6:
Just update the NUGET packages from the Nuget Package Manager.
回答7:
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
回答8:
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.
回答9:
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
回答10:
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.
回答11:
What fixed it for me after I couldn't scaffold IdentityFramework was by
- Checking VS2019 for updates.
- Update NuGet Packages (Tools -> Nuget Package Manager -> Manage NuGet packages for solution -> Click on the updates tab, select all and run update.
- Retry scaffolding identity
回答12:
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
回答13:
My solution -Vs2019 all nuget packages upgrade
回答14:
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.
Thanks and regards
回答15:
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.
来源:https://stackoverflow.com/questions/44509694/error-package-restore-failed