[英]How to change the NuGet-package reference in a Visual Studio project to use Nuget.config
I have a Visual Studio project file with the extension .csproj.我有一个扩展名为 .csproj 的 Visual Studio 项目文件。 Inside it are references like this:
里面是这样的引用:
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props"....
I have now made a NuGet.config file in the parent folder of from the solution folder.我现在已经在解决方案文件夹的父文件夹中创建了一个 NuGet.config 文件。 And I removed the local "packages" folder.
我删除了本地“包”文件夹。 In the new nuget.config I set up a common location for storing packages.
在新的 nuget.config 中,我设置了一个用于存储包的公共位置。
nuget.config: nuget.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="D:\Data\NuGet" />
</config>
<packageRestore>
<add key="enabled" value="True" />
</packageRestore>
</configuration>
When I building I now get this error:当我构建时,我现在收到此错误:
This project references NuGet package(s) that are missing on this computer.
此项目引用此计算机上缺少的 NuGet 包。 Use NuGet Package Restore to download them.
使用 NuGet Package Restore 来下载它们。 For more information... The missing file is ..\\packages\\Microsoft.Net.Compilers.1.0.0\\build\\Microsoft.Net.Compilers.props.
有关详细信息... 丢失的文件是 ..\\packages\\Microsoft.Net.Compilers.1.0.0\\build\\Microsoft.Net.Compilers.props。
How can I solve this?我该如何解决这个问题?
If I manually have to replace the (Import Project="..\\packages...) elements in the project file, what should I change it to, so that it follows the configuration from the Nuget.config?如果我必须手动替换项目文件中的 (Import Project="..\\packages...) 元素,我应该将其更改为什么,以便它遵循 Nuget.config 中的配置?
If I manually have to replace the (Import Project="..\\packages...) elements in the project file, what should I change it to, so that it follows the configuration from the Nuget.config?
如果我必须手动替换项目文件中的 (Import Project="..\\packages...) 元素,我应该将其更改为什么,以便它遵循 Nuget.config 中的配置?
Since you use the new nuget.config
file which changed the path of the local nuget reference(like this <add key="repositoryPath" value="xxxxxx" />
).由于您使用了新的
nuget.config
文件,该文件更改了本地 nuget 引用的路径(例如<add key="repositoryPath" value="xxxxxx" />
)。
And Restore will only restore the missing nuget packages but will not change to use the new nuget package location in xxx.csproj
.和恢复将仅恢复丢失的NuGet软件包,但不会改变在使用新的NuGet包的位置
xxx.csproj
。
So you can follow my steps to resolve the issue:所以你可以按照我的步骤来解决这个问题:
Solution解决方案
1) Tools-->Nuget Package Manager-->Package Manager Console--> 1)工具-->Nuget 包管理器-->包管理器控制台-->
type Update-Package -reinstall
to reinstall these packages to reference the new right path.键入
Update-Package -reinstall
以重新安装这些包以引用新的正确路径。
2) enter the xxxx.csproj
file, delete these duplicate, old import
info like these: 2)进入
xxxx.csproj
文件,删除这些重复的旧import
信息,如下所示:
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\..\..\..\..\..\installed_packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props'))" />
3) Then rebuild your project and will solve this issue. 3)然后重建你的项目,将解决这个问题。
Update 1更新 1
The new Nuget.config
file will specify that the newly installed nuget packages use the new reference address, but for previously installed nuget packages, the reference address in the xxx.csporj
file will remain the old address.新的
Nuget.config
文件将指定新安装的 nuget 包使用新的引用地址,但对于以前安装的 nuget 包, xxx.csporj
文件中的引用地址将保持旧地址。 The Restore procedure only restores the nuget package under the new path, but it does not make any changes to the nuget reference in xxx.csproj
file, so it can only be reinstalled.还原过程只还原新路径下的nuget包,但不会对
xxx.csproj
文件中的nuget引用做任何改动,所以只能重新安装。
Besides , the import node is created by Microsoft.Net.Compilers
props file from the build
folder in the microsoft.net.compilers
nuget package.此外,导入节点由
microsoft.net.compilers
nuget 包中的build
文件夹中的Microsoft.Net.Compilers
props 文件创建。 And it is a nuget mechanism which can do some operation in xxx.csproj
file when you install the nuget package. 它是一种nuget机制,可以在安装nuget包时在
xxx.csproj
文件中进行一些操作。
However , this file is also special and when you change the nuget reference path.但是,此文件也很特殊,当您更改 nuget 引用路径时。 Because nuget enabled the new address mechanism, during the uninstallation process, the old address of
Microsoft.Net.Compilers.props
is still not recognized, so it cannot be uninstalled.由于nuget启用了新地址机制,在卸载过程中,仍然无法识别
Microsoft.Net.Compilers.props
的旧地址,因此无法卸载。 In fact , when you execute the reinstall nuget package, a new address has been created in the xxx.csproj
file.其实在执行reinstall nuget包的时候,已经在
xxx.csproj
文件中新建了一个地址。 See this:看到这个:
So you should just delete duplicate files from the old address.所以你应该从旧地址中删除重复的文件。
Visual Studio option to change the Nuget Package References用于更改 Nuget 包引用的 Visual Studio 选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.