Search code examples
nuget

Can't update or install package: An item with the same key has already been added


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

Solution

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