简体   繁体   English

错误:“运行所选代码生成器时出错:Package 恢复失败”

[英]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:我正在尝试将 controller 添加到我在 ASP.NET 核心项目中的解决方案中:

When I try to do so I get this error:当我尝试这样做时,我收到此错误:

I get the same message for adding minimal dependencies and full dependencies for controller.对于为 controller 添加最小依赖项和完全依赖项,我收到相同的消息。

I also had this issue.我也有这个问题。 "Add Controller>API Controller with actions, using Entity Framework" would give the "Package Restore Failed" error. “使用实体框架添加控制器>带有操作的 API 控制器”会给出“包恢复失败”错误。

As Anish stated, it seems to be due to package versions being mis-aligned.正如 Anish 所说,这似乎是由于软件包版本未对齐。 I was able to resolve this issue using "Manage NUGET Packages for Solution", then performing an "Update All".我能够使用“管理解决方案的 NUGET 包”解决此问题,然后执行“全部更新”。 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".这将我的 AspNetCore 版本设置为 2.1.5 并解决了我的“Package Restore Failed”错误,但随后导致了另一个错误“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.显然,脚手架代码生成器需要 AspNetCore 和 NETCore 版本同步,所以我从 Microsoft 下载手动下载并安装了 NETCore 2.1.5 版。 This worked, and I was finally able to generate Controllers.这奏效了,我终于能够生成控制器。

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.实际上,VS只有离线包源,无法解析需要的包。

Add the online reference: Tools > nuget package manager > package manager settings > Package Sources添加在线参考:工具> nuget 包管理器> 包管理器设置> 包源

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

  1. VS2019 [5.0]. 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.更新 NuGet 包(工具 -> Nuget 包管理器 -> 管理解决方案的 NuGet 包 -> 单击更新选项卡,选择所有并运行更新。

  3. Solution -> Clean解决方案 -> 清洁

  4. solution -> Build解决方案 -> 构建

  5. create a Controller.创建一个控制器。

I try everything but the above method work for me我尝试了一切,但上述方法对我有用

I encountered this issue with net5.0, specifically against version 5.0.5 of some dependencies.我在使用 net5.0 时遇到了这个问题,特别是针对某些依赖项的 5.0.5 版。 I downgraded my nuget packages from 5.0.5 to 5.0.4 for these:我将我的 nuget 包从 5.0.5 降级到 5.0.4:

"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"

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.我最终通过查看我的解决方案中包含的每个单独的 .csproj 文件并修复包含的所有版本的 microsoft 库来解决它。

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.我将我引用的元包从“Microsoft.AspNetCore.All”更改为“Microsoft.AspNetCore.App”,然后我在 nuget 上加载了“App”包的引用列表,并删除了对已包含的库的任何引用在元包中。

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.然后我确保我修复了任何未完成包的版本以匹配项目自动选择的元包版本,即在我的情况下为 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>

I just had this problem whilst adding a controller to a Core API with Entity Framework project.我在使用 Entity Framework 项目向核心 API 添加控制器时遇到了这个问题。 I'm using VS 16.8.5 with the most recent EF core, version 5.03.我将 VS 16.8.5 与最新的 EF 核心版本 5.03 一起使用。 The class containing my DBContext class referenced EF 5.03 .包含我的 DBContext 类的类引用了 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.我(最终!)在浏览 Nuget 时注意到各种代码生成包(我的 .csproj 文件中都没有引用这些包,我认为是因为 ASP.Net 核心从 3.0 开始就作为框架提供,但如果我错了,请纠正我!) ,尤其是 Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCode 是 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.我没有触及我的 ASP.Net 项目,而是将其他 EF 项目降级到 5.02 并解决了问题。

只需从 Nuget 包管理器更新 NUGET 包。

I had the problem with a blazor server application version 5.0.5 and Microsoft Identity scaffolding.我遇到了 blazor 服务器应用程序版本 5.0.5 和 Microsoft Identity 脚手架的问题。 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. CodeGeneration.Design 包的最高可用版本是 5.0.2,所以我将其他 Microsoft 包(特别是 EntityFramework)降级到 5.0.2 并解决了问题。

Had exactly same problem, in my situation CodeGenerator was missing有完全相同的问题,在我的情况下缺少 CodeGenerator

I have added this item into ItemGroup in .csproj我已将此项目添加到 .csproj 中的 ItemGroup

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

What fixed it for me after I couldn't scaffold IdentityFramework was by在我无法搭建 IdentityFramework 之后为我修复它的是

  1. Checking VS2019 for updates.检查 VS2019 的更新。
  2. Update NuGet Packages (Tools -> Nuget Package Manager -> Manage NuGet packages for solution -> Click on the updates tab, select all and run update.更新 NuGet 包(工具 -> Nuget 包管理器 -> 管理解决方案的 NuGet 包 -> 单击更新选项卡,全选并运行更新。
  3. Retry scaffolding identity重试脚手架标识

I had resolved it by update two files我已经通过更新两个文件解决了

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

. . Its version should be match with other packages version in application.它的版本应该与应用程序中的其他包版本匹配。

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.并更新 NuGet 包(工具 -> Nuget 包管理器 -> 管理解决方案的 NuGet 包 -> 单击已安装。您需要选择版本并注意依赖项。

Example my option is fine with:例如我的选择很好:

Microsoft.AspNetCore.Identity.EntityFramework Core with version 3.1.12 Microsoft.AspNetCore.Identity.EntityFramework Core 版本 3.1.12

Microsoft.EntityFrameworkCore.Tools with version 3.1.12 Microsoft.EntityFrameworkCore.Tools 版本 3.1.12

Microsoft.EntityFrameworkCore.SqlServer with version 3.1.12 Microsoft.EntityFrameworkCore.SqlServer 版本 3.1.12

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

I am also facing this issue.我也面临这个问题。

Please follow this step,请按照此步骤,

  • Clean Your Solution清洁您的解决方案
  • Open Nuget Manager打开 Nuget 管理器
  • Check this version Microsoft.EntityFrameworkCore (I am using 5.0.8)检查此版本 Microsoft.EntityFrameworkCore(我使用的是 5.0.8)
  • Check this version Microsoft.EntityFrameworkCore.Design (I am using 5.0.8)检查此版本 Microsoft.EntityFrameworkCore.Design(我使用的是 5.0.8)
  • Check this version Microsoft.EntityFrameworkCore.Tools (I am using 5.0.8)检查此版本 Microsoft.EntityFrameworkCore.Tools(我使用的是 5.0.8)
  • Check this version Microsoft.EntityFrameworkCore.SqlServer (I am using 5.0.8)检查此版本 Microsoft.EntityFrameworkCore.SqlServer (我使用的是 5.0.8)
  • Check this version Microsoft.VisualStudio.Web.CodeGeneration.Design (I am using 5.0.2)检查此版本 Microsoft.VisualStudio.Web.CodeGeneration.Design(我使用的是 5.0.2)

也检查这张图片

  • After that Rebuild your solution and Create Scaffolding Controller之后重建您的解决方案并创建脚手架 Controller

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) 1 - 检查您的解决方案是否有多个项目,请检查他们的目标 Dot.net 框架。(在我的情况下,类库是 .Net Standard 1.6,Web 项目是 .NetCoreApp 1.0,我将其更改为 .NetCoreApp 1.1)

2 - After having the same framework, clean the web project, Rebuilt and Add new Controller. 2 - 拥有相同的框架后,清理 web 项目,重建并添加新的控制器。

If its successful fine otherwise You might encounter another error eg如果成功,否则您可能会遇到另一个错误,例如

'There was an error running the code generator: 'No executable found matching command "dotnet-aspnet-codegenerator"' '运行代码生成器时出错:'找不到匹配命令“dotnet-aspnet-codegenerator”的可执行文件'

If you have project.json file open it other wise open .csproj.user project in note pad, please add following如果您有project.json文件打开它,否则在记事本中打开.csproj.user项目,请添加以下内容 Please note based on your .net version you might have different version no.请注意,根据您的 .net 版本,您可能有不同的版本号。

You may find instruction in ScaffoldingReadMe.txt file if its generated in your project如果在您的项目中生成了ScaffoldingReadMe.txt文件,您可能会在其中找到说明

All I had to do was open the properties of my web project and change the TargetFramework from 2.1 to 2.2.我所要做的就是打开我的 web 项目的属性并将 TargetFramework 从 2.1 更改为 2.2。 Or to match whatever version of the framework your business and object layer are using.或者匹配您的业务和对象层正在使用的任何版本的框架。

Had the same problem, but updating all the NuGet Packages has solved the problem.有同样的问题,但更新所有 NuGet 包已经解决了问题。 Right click on < your project name > file -> Manage NuGet Packages -> Updates -> Select all packages -> Update右键单击<您的项目名称> 文件 -> 管理 NuGet 包 -> 更新 -> 选择所有包 -> 更新

I'm running .NET Core (and Entity Framework Core) 3.1.x.我正在运行 .NET Core(和 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.我得到了这个确切的错误,并尝试更新这里的答案中已经提到的所有 nuget 包和其他相关解决方案。

The issue was simply that my database server was not running (it runs on a local VM).问题只是我的数据库服务器没有运行(它在本地 VM 上运行)。 In other words, my database context (ie ApplicationDbContext ) mentioned in the 'Add Controller...' window, was not able to access the db.换句话说,我在“添加控制器...”窗口中提到的数据库上下文(即ApplicationDbContext )无法访问数据库。 Once I started the db server, my scaffolding created without issue.一旦我启动了数据库服务器,我的脚手架就没有问题了。

Keep also in mind, the model/class (ie table) that the controller and views were referencing had not been created yet (I hadn't run add-migration yet).还要记住,控制器和视图所引用的模型/类(即表)尚未创建(我还没有运行 add-migration)。 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.这是一种愚蠢的(显而易见的?)解决方案,但在查看“包还原失败”错误消息时非常具有误导性。

I had a similar issue with entity framework core sqlite nuget packages.我在实体框架核心 sqlite nuget 包中遇到了类似的问题。 I installed sqlite and sqlite core packages fixed this.我安装了 sqlite 和 sqlite 核心包修复了这个问题。 Probably a needy package is missing.可能缺少一个需要的包裹。 Also make sure SQL Server and Server Agent are running.还要确保 SQL Server 和服务器代理正在运行。 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.检查SQL Server 配置> SQL Server 服务> 右键单击SQL Server 或服务器代理并启动服务,然后重新启动服务器。 Guess this might help someone猜猜这可能会帮助某人

我的解决方案-Vs2019所有nuget包升级

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.问题是您的项目中安装了一些旧版本的 nuget pacakges,当您尝试搭建脚手架时,Asp.net 核心会尝试安装此时搭建脚手架所需的最新包,Asp.net 核心会抛出此类异常.

I suggest you to update your nuget packages and than try scafolding.我建议您更新您的 nuget 软件包,而不是尝试搭建脚架。

Thanks and regards谢谢并恭祝安康

As someone mentioned earlier, I updated all NuGet packages from Tools->Nuget Package Manager -> Manage NuGet Packages for Solution -> Updates tab -> Update All.正如前面有人提到的,我从工具-> Nuget 包管理器 -> 管理解决方案的 NuGet 包 -> 更新选项卡 -> 全部更新更新了所有 NuGet 包。 I was then able to add a controller with EF and have VS generate the associated views.然后我能够添加一个带有 EF 的控制器并让 VS 生成相关的视图。

Just update all Nuget Packages , Clean Solution and the rebuild solution.只需更新所有 Nuget 包、清洁解决方案和重建解决方案。 It solve the issues for me.它为我解决了问题。

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.对于我有多个项目的 sln,有net 5 和一些 5.0.5 的 NuGet 包和一些 5.0.2 ,只有通过命令行的代码生成器工作。 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

I just updated EntityFrameworkCore from Version 3.1.10 to 3.1.13 and it solved the problem.我刚刚将 EntityFrameworkCore 从版本 3.1.10 更新到 3.1.13,它解决了这个问题。 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>

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.我设法通过删除 csproj 文件中<ItemGroup>标记中的所有内容并运行代码生成器来使其工作。 The generator then installs packages that it needs.生成器然后安装它需要的包。

Cleaning the solution showed me an error of NuGet packages needed to be updated!清理解决方案显示需要更新 NuGet 包的错误! I updated them and build the solution.我更新了它们并构建了解决方案。 Build Successful and I was able to create the controller class.构建成功,我能够创建控制器类。

Trying to add MVC controller with views using EF for MVC project using Net5.0 .尝试使用 EF 为使用Net5.0 MVC 项目添加带有视图的 MVC 控制器。

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.使用以下特定版本的NuGet包对我有用,而问题是通过使用比Microsoft.EntityFrameworkCore版本少的版本解决的,对于Microsoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.Tools ,这些包在 MVC 项目中被引用.

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>

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).我通过更新两个主要关注项目的 nuget 包来解决这个问题(我的意思是这两个项目与我要实施的项目有关)。

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.如果您使用的是 .net 5.0.14,将版本为 5.0.14 的每个软件包从 5.0.14 降级到 5.0.12可以解决我的问题。

I know that some of you might still facing the same issue, I just did the next in VS 2022,我知道你们中的一些人可能仍然面临同样的问题,我刚刚在 VS 2022 中做了下一个,

1- Checked the depencendies on the current project. 1-检查当前项目的依赖关系。 在此处输入图像描述

2- Remove all of them 2-删除所有这些

3- Go to dependencies add a lower version 3- Go 到依赖项添加较低版本

在此处输入图像描述

and the clean the solution and add the views.并清理解决方案并添加视图。

暂无
暂无

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

相关问题 运行选定的编码生成器时出现错误:“包恢复失败。没有详细错误 - There was and error running the selected coded Generator: 'Package restore Failed.no detail error 运行所选生成器时出错:包还原失败。回滚“MyProject”的包更改(Asp.net 核心项目错误)) - There was an error running the selected generator: Package restore failed.Rolling back package changes for 'MyProject' (Asp.net core Project Error)) 运行所选代码生成器时出错: - There was an error running the selected code generator: 在 .net6 中运行选定的代码生成器时出错 - there was an error running the selected code generator in .net6 Web Api 脚手架 - 运行所选代码生成器时出错 - Web Api Scaffolding - Error running the selected code generator VS 2013 脚手架中的“运行选定的代码生成器时出错” - "There was an error running the selected code generator" in VS 2013 scaffolding 运行所选代码生成器时出错:没有为类型定义无参数构造函数 - There was an error running the selected code generator: No Parameterless constructor defined for type 运行选定的生成器时出错。 尝试重建项目 - There was an error running the selected generator . try rebuilding the project 运行所选代码生成器时出错:“未将对象引用设置为对象的实例。” 错误? - There was an error running the selected code generator: 'Object reference not set to an instance of an object.' Error? Asp.net核心使用实体框架添加支架式剃须刀页面:运行所选代码生成器时出错 - Asp.net core add scaffolded, razor pages using entity framework: there was an error running the selected code generator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM