首页 > 解决方案 > Nuget Restore 在不同位置创建包文件夹

问题描述

我有一个包含类库的解决方案 A,它有一个 package.config。当我使用 Nuget 还原选项时,它会正确地重新创建包文件夹并且引用正常。问题出现在另一个解决方案 B 中,我添加了项目 A,当我尝试 Nuget Restore 时,它​​在解决方案 B 目录中创建了 packages 文件夹,而项目 A 找不到丢失的引用。如何使解决方案 B 恢复项目 A 目录中项目 A 所需的包?

标签: nugetnuget-package-restore

解决方案


TL;DR: Try running Update-Package -Reinstall from the Package Manager Console.

The only way I know for this to happen is when a project that uses packages.config for NuGet references is moved to a new directory with a different relative path to the solution. packages.config is basically "non-integrated" package management. Anyone using .NET since the beginning will remember when there was no NuGet and you'd need to add assembly references directly to dlls. packages.config automates this, but means that when the path to the solution packages folder changes, the references are no longer valid. NuGet doesn't know this and always restores to the solution packages folder. So, you need to update the project to point correctly to the solution packages folder. You can do it manually by editing your project file, by uninstalling and re-installing all the packages one-by-one, or use PMC's Update-Package to do it.

PackageReference doesn't have this problem, as the path to the restored packages is not stored in the project, so even if your project moves around, worst case all you need to do is restore again, but often even that's not needed. So, I recommend you migrate from packages.config to PackageReference if you can, but see the docs because there are package compatibility issues to be aware of. Basically, if you use packages with content that gets copied into your project on install, you need to stick with packages.config. Otherwise PackageReference gives a better experience to almost everyone else.


推荐阅读