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"?
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"
}
}
}
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.
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" />
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.config
either.
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.
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"
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.
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.
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).
© 2022 - 2025 — McMap. All rights reserved.