187

I have solution & project in Visual Studio 2012.

The project has a file packages.config in the root of the project.

For the purposes of this question, lets assume I accidentally removed these libraries from the References section of my project.

When going into NuGet package manager, the interface is still reporting a tick next to these packages, indicating they are installed.

The only way I can see how to fix this situation is to delete all the entries from packages.config, which will fix the issue of the NuGet interface reporting them as installed, and re-add each one.

Is there a smarter way? I had hoped enabling 'enable nuget to restore missing packages' would solve this, but it doesnt seem to do anything.

maxp
  • 24,209
  • 39
  • 123
  • 201

13 Answers13

339

Try re-installing the packages.

In the NuGet Package Manager Console enter the following command:

Update-Package -Reinstall -ProjectName Your.Project.Name

If you want to re-install packages and restore references for the whole solution omit the -ProjectName parameter.

jmfenoll
  • 3,575
  • 1
  • 13
  • 5
  • 10
    English translation of the link: http://translate.google.com/translate?sl=es&tl=en&js=n&prev=_t&hl=en&ie=UTF-8&eotf=1&u=http%3A%2F%2Fwww.overflowexception.es%2F2013%2F03%2Freinstalar-paquetes-de-nuget.html – Csaba Toth May 21 '13 at 21:39
  • 18
    Note: This command will reinstall the references on *all* projects that are currently open in Visual Studio, rather than just the project selected in the console. – simbolo Oct 28 '13 at 18:24
  • 3
    This command will reinstall packages in whole solution, not only in a selected project! – Alex Sorokoletov Jun 10 '14 at 04:08
  • 14
    Extremelly Dangerous!!!! If the process gets interrupted, you will loss all your package references and will need to add one-by-one to every project in your solution. – Bill Velasquez Aug 13 '14 at 14:54
  • 2
    @BillVelasquez this thing just ate all my references. Well, thank god for git, I guess. – Gleno Aug 31 '14 at 21:43
  • 2
    Take note of the `-Project` switch which *will* only reinstall for the specific project identified. – atconway Oct 09 '14 at 02:48
  • The parameter -ProjectName XXXX is important in my case. Without that Nuget won't do anything on my specific project. – Minh Nguyen Mar 04 '15 at 10:27
  • @Al-Muhandis: May be was due to some internet problems, because for me 30 packages were re-installed for a project in a approximately a couple of minutes as I targeted a new .net framework version. – Syed Waqas Feb 24 '17 at 07:07
  • 1
    @Bill -- If you're not using version control, today might be a good day to start. In my opinion, the ability to undo changes or even scrap an environment and start fresh is almost essential. Check out TFS or Git. – Jonathan Jul 20 '17 at 11:33
  • 1
    Another vital warning: this will not just reinstall the packages but upgrade you to later versions if they are available. – Daniel Earwicker Feb 16 '18 at 13:49
  • Should have read this last comment before running the command...Nuget packages get updated! What to do if I only want to restore? – harveyAJ Aug 05 '20 at 10:41
45

You need to Enable NuGet package restore at the VS solution level for the restore missing package to work.

enter image description here

Gan
  • 4,827
  • 3
  • 34
  • 50
  • 43
    This will allow NuGet to re-download missing packages into the .\packages\ directory, but has nothing to do with restoring missing *project references*, which is what the poster was asking about. – Brant Bobby Jan 22 '13 at 20:12
13

In case this helps someone, for me, none of the above was sufficient. I still couldn't build, VS still couldn't find the references. The key was simply to close and reopen the solution after restoring the packages.

Here's the scenario (using Visual Studio 2012):

You open a Solution that has missing packages. The references show that VS can't find them. There are many ways to restore the missing packages, including

  • building a solution that is set to auto-restore
  • opening the Package Manager Console and clicking the nice "Restore" button
  • doing nuget restore if you have the command line nuget installed

But no matter what the approach, those references will still be shown as missing. And when you build it will fail. Sigh. However if you close the solution and re-open it, now VS checks those nice <HintPath>s again, finds that the packages are back where they belong, and all is well with the world.

Update

Is Visual Studio still not seeing that you have the package? Still showing a reference that it can't resolve? Make sure that the version of package you restored is exactly the same as the <HintPath> in your .csproj file. Even a minor bug fix number (e.g. 1.10.1 to 1.10.2) will cause the reference to fail. You can fix this either by directly editing your csproj xml, or else by removing the reference and making a new one pointing at the newly-restored version in the packages directory.

Community
  • 1
  • 1
John Hatton
  • 1,734
  • 18
  • 20
  • 1
    You make a very important point about the restore only ensuring the packages are in the package folder (which of course can be many places). However closing and re-opening was still not working for me even with the correct package versions, I ended up having to manually amend the hint paths in each csproj file. I believe this is caused by moving the package folder relative to the project. – Shaun Feb 26 '15 at 15:48
  • Editing the `.csproj` file to make sure version numbers matched worked for me. Thanks! – Mateen Ulhaq Feb 12 '16 at 20:08
11

While the solution provided by @jmfenoll works, it updates to the latest packages. In my case, having installed beta2 (prerelease) it updated all of the libs to RC1 (which had a bug). Thus the above solution does only half of the job.

If you are in the same situation as I am and you would like to synchronize your project with the exact version of the NuGet packages you have/or specified in your packages.config, then, then this script might help you. Simply copy&paste it into your Package Manager Console

function Sync-References([string]$PackageId) {
  get-project -all | %{
    $proj = $_ ;
    Write-Host $proj.name; 
    get-package -project $proj.name | ? { $_.id -match $PackageId } | % { 
      Write-Host $_.id; 
      uninstall-package -projectname $proj.name -id $_.id -version $_.version -RemoveDependencies -force ;
      install-package -projectname $proj.name -id $_.id -version $_.version
    }
  }
}

And then execute it either with a sepific package name like

Sync-References AutoMapper

or for all packages like

Sync-References

Credits go to Dan Haywood and his blog post.

Juri
  • 32,424
  • 20
  • 102
  • 136
8

The following script can be run in the Package Manger Console window, and will remove all packages from each project in your solution before reinstalling them.

foreach ($project in Get-Project -All) { 
    $packages = Get-Package -ProjectName $project.ProjectName
    foreach ($package in $packages) {
        Uninstall-Package $package.Id -Force -ProjectName $project.ProjectName
    }
    foreach ($package in $packages) {
        Install-Package $package.Id -ProjectName $project.ProjectName -Version $package.Version
    }
}

This will run every package's install script again, which should restore the missing assembly references. Unfortunately, all the other stuff that install scripts can do -- like creating files and modifying configs -- will also happen again. You'll probably want to start with a clean working copy, and use your SCM tool to pick and choose what changes in your project to keep and which to ignore.

Brant Bobby
  • 14,956
  • 14
  • 78
  • 115
4

This script will reinstall all packages of a project without messing up dependencies or installing dependencies that may have been intentianlyz removed. (More for their part package developers.)

Update-Package -Reinstall -ProjectName Proteus.Package.LinkedContent -IgnoreDependencies
Jeremy
  • 41
  • 1
3

I added the DLLs manually. Right clicked on References in the project, select Add Reference and then in the dialog pressed the Browse button. The NuGet DLLs where in the packages directory of the solution. To get the names of them you can right click on references in another project that's working properly and select properties and look in the path property.

Mark Horgan
  • 3,243
  • 4
  • 27
  • 28
2

In Visual Studio 2015 (Soulution is under source control, MVC-Project), csano's Update-Package -Reinstall -ProjectName Your.Project.Name worked, but it messed up with some write locks.

I had to delete the "packages"-Folder manually before. (It seemed to be locked because of the source control).

Also, I had to re-install the MVC-Package from the NuGet Package Manager.

Yves
  • 51
  • 7
1

Just in case it helps someone - In my scenario, I have some shared libraries (Which have their own TFS projects/solutions) all combined into one solution.

Nuget would restore projects successfully, but the DLL would be missing.

The underlying issue was that, whilst your solution has its own packages folder and has restored them correctly to that folder, the project file (e.g. .csproj) is referencing a different project which may not have the package downloaded. Open the file in a text editor to see where your references are coming from.

This can occur when managing packages on different interlinked shared solutions - since you probably want to make sure all DLLs are on the same level you might set this at the top level. This means it that sometimes it will be looking in a completely different solution for a referenced DLL and so if you don't have all projects/solutions downloaded and up-to-date then you may get the above problem.

McGaz
  • 1,354
  • 1
  • 13
  • 22
1

I have to agree with @Juri that the vastly popular answer by jmfenoll is not complete. In the case of broken references, I submit that most of the time you do not want to update to the latest package, but only fix your references to the current versions that you happen to be using. And Juri provided a handy function Sync-References to do just that.

But we can go just a bit further, allowing the flexibility to filter by project as well as package:

function Sync-References([string]$PackageId, [string]$ProjectName) {
    get-project -all | 
    Where-Object { $_.name -match $ProjectName } |
    ForEach-Object {
        $proj = $_ ;
        Write-Output ('Project: ' + $proj.name)
        Get-Package -project $proj.name |
        Where-Object { $_.id -match $PackageId } |
        ForEach-Object { 
            Write-Output ('Package: ' + $_.id)
            uninstall-package -projectname $proj.name -id $_.id -version $_.version -RemoveDependencies -force
            install-package -projectname $proj.name -id $_.id -version $_.version
        }
    }
}
Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
1

I had the same issue with missing references. Below my scenario:

  • Fresh Windows 10 machine and VS Community 2015 install
  • Just checked out repository code via TFS
  • One solution built just fine, one solution had one project with missing references (EF, System.Http, as instance), but the relative nuget packages were properly installed.

All version numbers in project and packages match, doing nuget restore (in all its ways) did not work.

How I fixed it: simply delete the package folders in the solution root and execute nuget restore. At this point the dlls are correctly downloaded and can be added for the missing references.

Francesco
  • 9,947
  • 7
  • 67
  • 110
0
  1. Copy packages.config file of project and applies all version modify
  2. Unistall all package and remove depencies

    $packages = Get-Package -ProjectName [nameOfProjectToRestore]
    foreach ($package in $packages) {
        uninstall-package  -projectname [nameOfProjectToRestore] -id $package.Id -version $package.version -RemoveDependencies -force ;
    }
    
  3. Clear the packages folder in the root of project

  4. Copy the modifies package.config to root folder of website

  5. Run this code to restore the project

    $packages = Get-Package -ProjectName [nameOfProjectToRestore]
    foreach ($package in $packages) {
        uninstall-package  -projectname [nameOfProjectToRestore] -id $package.Id -version $package.version -RemoveDependencies -force ;
    }
    foreach ($package in $packages) {
        install-package  $package.Id -ProjectName [nameOfProjectToRestore] -Version $package.Version
    }
    
Koopakiller
  • 2,838
  • 3
  • 32
  • 47
Lluthus
  • 335
  • 2
  • 5
  • 15
0

I suffered from this issue too a lot, in my case Downloading missing NuGet was checked (but it is not restoring them) and i can not uninstall & re-install because i modified some of the installed packages ... so:

I just cleared the cached and rebuild and it worked. (Tools-Option-Nuget Package Manager - General)

also this link helps https://docs.nuget.org/consume/package-restore/migrating-to-automatic-package-restore.

Mamdouh
  • 186
  • 2
  • 7