Can't update or install package: An item with

2020-08-11 19:49发布

问题:

Problem

In a particular project, I can't update or install any NuGet packages. When I try to do so using the NuGet GUI, it does some work and then stops without saying anything. When I try to do so using the package manager console, I get this output:

PM> Update-Package –reinstall EntityFramework
Attempting to gather dependencies information for multiple packages with respect to project 'SmartCentre', targeting '.NETFramework,Version=v4.5.2'
Update-Package : An item with the same key has already been added.
At line:1 char:15
+ Update-Package <<<<  –reinstall EntityFramework
    + CategoryInfo          : NotSpecified: (:) [Update-Package], Exception
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.UpdatePackageCommand

Environment

  • Visual Studio Professional 2015 Update 1
  • NuGet 3.3.0.167

What I've tried

  • Deleting the packages folder
  • Restarting Visual Studio
  • Restarting the computer

回答1:

It turned out that the packages.config file had a couple of duplicates with different versions:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <!-- ... -->
  <package id="Microsoft.AspNet.Web.Optimization" version="1.0.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.2" targetFramework="net40" />
  <!-- ... -->
  <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
  <package id="Newtonsoft.Json" version="5.0.8" targetFramework="net40" />
  <!-- ... -->
</packages>

Once I removed the duplicates, the problems stopped happening:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <!-- ... -->
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.2" targetFramework="net40" />
  <!-- ... -->
  <package id="Newtonsoft.Json" version="5.0.8" targetFramework="net40" />
  <!-- ... -->
</packages>


回答2:

If any one is using net core and got this error use command prompt to get detailed error message.

Please run dotnet restore to find the conflicting versions.

Above command need to run form the folder where pjt file resides.

Ex:

 Unable to satisfy conflicting requests for '************': (>= 1.0.1)

 (via package/************ 1.0.22), ************ (>= 1.0.1)

 (via project/************ 1.0.0) Framework: 

 (.NETStandard,Version=v2.0)C:\Users\************\Source\************API.csproj]


回答3:

In my case, i have different packages.config files (one in pcl, one in droid and one in ios), with different package versions. In my packages folder ( Projects\MyProjName\packages ), i have this situations:


I removed only old versions of duplicates and after this i update data in package.config files with right version for example:

 <package id="Newtonsoft.Json" version="8.0.3" targetFramework="net40" />

became

<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net40" />

Now you can re open project and all works fine.



回答4:

In my situation, we had a bad nuget.config file checked into our repo. It had settings pointing to two different NuGet repositories that both contained the same packages. We were using NuGet.exe 3.5. Deleting the nuget.config file fixed this issue for us.



回答5:

I tried everything and then;

  1. Made sure the Framework was up to date. (for the MVC package I was upgrading)
  2. Uninstalled NuGet and reinstalled it.

It might be worth a try. Doesn't take too long.



回答6:

I somehow added two different versions of import references to my csproj file. I had manually edit it to remove the extra reference.

From:

<Import Project="..\packages\Xamarin.Forms.2.5.0.121934\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets" Condition="Exists('..\packages\Xamarin.Forms.2.5.0.121934\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets')" />
<Import Project="..\packages\Xamarin.Forms.3.1.0.583944\build\netstandard2.0\Xamarin.Forms.targets" Condition="Exists('..\packages\Xamarin.Forms.3.1.0.583944\build\netstandard2.0\Xamarin.Forms.targets')" />

To:

<Import Project="..\packages\Xamarin.Forms.3.1.0.583944\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets" Condition="Exists('..\packages\Xamarin.Forms.3.1.0.583944\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets')" />
<Import Project="..\packages\Xamarin.Forms.v\build\netstandard2.0\Xamarin.Forms.targets" Condition="Exists('..\packages\Xamarin.Forms.3.1.0.583944\build\netstandard2.0\Xamarin.Forms.targets')" />

All good.



回答7:

The same problem encountered on my side. Uninstall-package followed by Install-package resolved the problem.



回答8:

In my case, I had packaged multiple assemblies in a custom NuGet package and bumped the version of the primary library, but had forgotten to update the secondary assembly.

My.MainPackage.dll v 1.6 <-- Bumped the version here
My.PackageDependency v 1.5 <-- Forgotten to bump this version

After pushing this bad package, whenever I attempted to Update-Package any project using the previous version, I would get the error

An item with the same key has already been added

Since both the previous and next releases of this package both contained an old version of the secondary assembly.

The solution was simply to rebuild the broken package, bumping both assembly versions again.

FWIW any downstream projects which I had attempted to upgrade with this bad package were left in a half-baked state and needed to be manually edited to get back to a consistent version.



回答9:

This error message is really extremely vague and can mean anything :(

I can recommend this approach for big projects:

  • Remove all projects from your solution
  • Readd them one by one until the error appears
  • Check the .csproj of the project you added last
    • Delete big sections from the .csproj
    • Try if the error still exists
    • Repeat until you can narrow down the section causing the issue (readding the previously deleted sections usually is a good idea)
    • Continue until you found something that seems like a duplicate

For me it was the following in my .csproj:

  <RuntimeIdentifier>win</RuntimeIdentifier>
  /* Some more stuff */
  <RuntimeIdentifiers>win;linux</RuntimeIdentifiers>

Removing <RuntimeIdentifier>win</RuntimeIdentifier> solved the problem.



标签: nuget