An item with the same key has already been added while Installing NuGet package
Asked Answered
P

8

19

In my project, I was using class library. Now I made that class lib as a NuGet package, remove the class lib and when try to install the package this error appears:"An item with the same key has already been added"?

Pull answered 14/7, 2015 at 12:4 Comment(0)
O
28

In my case, I saw this error when my packages.config file contained duplicate package ids which isn't allowed.

You can use the PowerShell script below to find all duplicate packages in your solution. It finds all packages.config files recursively and per packages.config file, it checks for duplicate package ids.

$solutionFolder = "C:\MySolution"
$nugetPackageFile = "packages.config"

$files = Get-ChildItem -Path $solutionFolder -Filter $nugetPackageFile -Recurse

foreach ($file in $files)
{
    [xml]$xml = Get-Content $file.FullName
    $nodes = Select-Xml "/packages/package/@id" $xml
    $packageIds = @{}

    foreach ($node in $nodes) {
        $packageId = $node.Node.'#text'
        try
        {
            $packageIds.Add($packageId, $packageId)
        }
        Catch [System.ArgumentException]
        {
            Write-Host "Found duplicate package in " $file.FullName ". Duplicate package: $packageId"
        }
    }
}
Overdose answered 15/7, 2015 at 15:1 Comment(4)
I have the same problem but after runnin this script console did not show anything. So i'm guessing in my case there are no duplicate but still installing or updating nuget packages show error' An item with the same key has already been added'Neighborhood
Yes there were duplicate keys in packages.config which needed to be removed. And crucially, not necessarily to do with the current Nuget Package you are using.Femininity
Found I had a duplicate key after a faulty merge, not a very good message!Stroy
Solve my issue, thanks. Also, for slow understanders (as I am) - it is not the already exists raw of new package presents in the file, it can be caused by duplicated entrances of ANY package name, for example 2 different version of the Newtonsoft.Json package.Reider
P
19

I had the same error and it got fixed after I upgraded NuGet itself. Use the Tools -> 'Extensions and Updates' dialog box to update NuGet.

Pronator answered 23/8, 2015 at 3:24 Comment(3)
This advice was amazing, VS2015 had been kinda buggy for me, but the NuGet Install-Package issues I was seeing resolved after doing a little house keeping of packages in Tools->Extensions&Updates. Thank you.Glob
Even if Package duplicates ARE the issue, updating NuGet to the latest version allows NuGet to deal with duplicates gracefully.Flaxen
In my ccase, i had the latest update (2016-09), but the dupllicate entry was the reason for the poroblemWomanhood
P
10

I've had this problem on a number of occasions when picking up code from another developer. The problems mentioned by other users are some of the things I've come across. So here are a list of things I've done in the past to solve this, and also a new one I've just come across:

  • Restart Visual Studio, NuGet sometimes references the wrong files for some reason (a very common situation and solution)
  • Update NuGet Manager in Tools > Extensions and Updates... (as mentioned by @ravinsp)
  • Clean and Rebuild your solution. Old dlls can mess things up (as mentioned by @Jules)
  • Check for duplicate references with DIFFERENT versions in packages.config - even though you are trying to install a completely different package, this error could be caused by another package issue. I was trying to install OctoPack, and got this error but it was being caused by System.Spatial. My packages.config had these two lines in it:

    <package id="System.Spatial" version="5.6.2" targetFramework="net45" />

    <package id="System.Spatial" version="5.6.4" targetFramework="net45" />

Palpitate answered 18/5, 2016 at 9:8 Comment(1)
The "Check for duplicate references with DIFFERENT versions in packages.config" option was the solution for me. thanks;Virgule
O
4

I had the same problem. It kept on telling me me that "An item with the same key has already been added", even though it was not in my references and not in my packages.configeither.

Eventually I managed to fix it by showing all files in Visual Studio. Inside the bin folder I then found a reference to the .dll I was trying to install through Nuget. After removing that one, the problem was gone.

Maybe this fixes it for you as well.

Outburst answered 10/11, 2015 at 16:34 Comment(1)
I would've loved to do some troubleshooting on this - but this fixed the issue for me also!Trigon
O
0

I also faced the same problem. I removed the package and removed following item from from Web.Config file, and then installed the package back - problem solved!

section name="ajaxControlToolkit" type="AjaxControlToolkit.AjaxControlToolkitConfigSection, AjaxControlToolkit"

Oxblood answered 4/5, 2016 at 9:17 Comment(0)
C
0

This had me going for most of the day. Same problem while trying to install a package. Finally tried installing a different package with the same result. Turns out that ANY duplications in the packages.config (with different versions) will cause this error. Removed the older versions of the two duplicates, and now I can install packages again.

Cacuminal answered 19/6, 2018 at 20:2 Comment(0)
C
0

My issue comes many years later... VS2019 and I get this error in .net 5.

I was getting this error while building an included project but that project had included a project that wasn't in the solution.

All projects I built gave the same error so it looked like it was the current set of nuget packages for whichever project I was building. Once I removed the project with the missing include or added the dependent project to the solution it was resolved.

Cortico answered 18/6, 2021 at 17:25 Comment(0)
S
0

In VS 2022, the latest (2024) version of Nuget may cause this issue if the solution has projects with the same name:

https://github.com/NuGet/Home/issues/13456

PS: If you attempt to migrate to Package Reference (when this is the case), the migration will fail (with no error message).

Swart answered 1/8, 2024 at 21:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.