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条回答
仙女界的扛把子
2楼-- · 2019-01-29 15:20

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.

查看更多
3楼-- · 2019-01-29 15:21
  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
    }
    
查看更多
何必那么认真
4楼-- · 2019-01-29 15:22

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.

查看更多
Bombasti
5楼-- · 2019-01-29 15:23

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
查看更多
闹够了就滚
6楼-- · 2019-01-29 15:24

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.

查看更多
闹够了就滚
7楼-- · 2019-01-29 15:25

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
        }
    }
}
查看更多
登录 后发表回答