Restoring Nuget References?

2019-01-29 14:54发布

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.

13条回答
Melony?
2楼-- · 2019-01-29 15:26

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.

查看更多
不美不萌又怎样
3楼-- · 2019-01-29 15:34

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

enter image description here

查看更多
欢心
4楼-- · 2019-01-29 15:37

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.

查看更多
男人必须洒脱
5楼-- · 2019-01-29 15:38

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.

查看更多
一夜七次
6楼-- · 2019-01-29 15:40

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.

查看更多
倾城 Initia
7楼-- · 2019-01-29 15:42

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.

查看更多
登录 后发表回答