Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

nuget

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

like image 661
user3583415 Avatar asked Jul 14 '15 12:07

user3583415


2 Answers

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"
        }
    }
}
like image 177
lvmeijer Avatar answered Oct 18 '22 17:10

lvmeijer


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.

like image 44
ravinsp Avatar answered Oct 18 '22 17:10

ravinsp