从 .Net 4.0 到 4.5 的重定向解决方案 - 如何重定向 NuGet 包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12006991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retargeting solution from .Net 4.0 to 4.5 - how to retarget the NuGet packages?
提问by Ivan Zlatev
I have migrated a solution that is currently targeting .NET 4.0 in VS2010 to VS2012 and now I would like to re-target it to .Net 4.5
我已将当前在 VS2010 中针对 .NET 4.0 的解决方案迁移到 VS2012,现在我想将其重新定位到 .Net 4.5
What I am not sure about is the NuGet packages. For example EF5, which I updated from EF4 in VS2010 turns out to be actually EF 4.4 as you can see here:
我不确定的是 NuGet 包。例如,我在 VS2010 中从 EF4 更新的 EF5 实际上是 EF 4.4,正如您在此处看到的:
<Reference Include="EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\EntityFramework.5.0.0\lib\net40\EntityFramework.dll</HintPath>
</Reference>
I can also see the following in packages.config for the project:
我还可以在项目的 packages.config 中看到以下内容:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="5.0.0" targetFramework="net40" />
</packages>
So my question is:
所以我的问题是:
What is the best practice to re-target all NuGet packages that are currently set to target .NET 4.0 to target .NET 4.5?
将当前设置为面向 .NET 4.0 的所有 NuGet 包重新定位到面向 .NET 4.5 的最佳实践是什么?
回答by Jeff Handley
NuGet 2.1offers a feature that makes this a lot simpler: just do update-package -reinstall -ignoreDependenciesfrom the Package Manager Console.
NuGet 2.1提供了一个功能,使这变得更简单:只需update-package -reinstall -ignoreDependencies从包管理器控制台执行即可。
NuGet 2.0doesn't handle re-targeting your applications very well. In order to change your packages' target frameworks, you must uninstall and reinstall the packages (taking note of the packages you had installed so that you can reinstall each of them).
NuGet 2.0不能很好地处理重新定位您的应用程序。为了更改包的目标框架,您必须卸载并重新安装包(记下您已安装的包,以便您可以重新安装每个包)。
The reason packages must be uninstalled and reinstalled is:
必须卸载并重新安装软件包的原因是:
- When installing a package, we determine the target framework of your project
- We then match that up with the package contents, finding the appropriate \lib\ folder (and \content\ folder)
- Assembly references are added with Hint Paths that point to the package's \lib\ folder, with the right subfolder (\lib\net40 for example)
- Content files are copied from the packages \content\ folder, with the right subfolder (\content\net40 for example)
- We record the targetFramework used to install the package within the packages.config file
- After you change your project's target framework, the Hint Paths still point to net40
- When you uninstall packages, we check the targetFramework that was recorded in packages.config to see what target framework's libs/content to remove from your project
- When you reinstall the package, we detect your updated target framework and reference/copy the right libs/content
- 安装包时,我们确定您项目的目标框架
- 然后我们将其与包内容进行匹配,找到适当的 \lib\ 文件夹(和 \content\ 文件夹)
- 程序集引用添加了指向包的 \lib\ 文件夹的提示路径,带有正确的子文件夹(例如 \lib\net40)
- 内容文件从包 \content\ 文件夹中复制,带有正确的子文件夹(例如 \content\net40)
- 我们在packages.config文件中记录了用于安装包的targetFramework
- 更改项目的目标框架后,提示路径仍指向 net40
- 当您卸载软件包时,我们会检查在 packages.config 中记录的 targetFramework 以查看要从您的项目中删除哪些目标框架的库/内容
- 当您重新安装包时,我们会检测您更新的目标框架并引用/复制正确的库/内容
回答by vpalmu
For those who had problems with update-package -reinstall <packagename>command, consider running it with -ignoreDependenciesflag, like this:
对于那些在update-package -reinstall <packagename>命令方面有问题的人,可以考虑使用-ignoreDependencies标志运行它,如下所示:
update-package -reinstall <packagename> -ignoreDependencies
This flag will leave your package dependencies alone, otherwise they might got updated even if the package you originally wanted reinstall still keeps it's version in same.
此标志将保留您的软件包依赖项,否则即使您最初想要重新安装的软件包仍然保持其版本不变,它们也可能会更新。
More info here.
更多信息在这里。
回答by Bo Sunesen
After trying the accepted answer unsuccessfully I would like to suggest a less risky command:
在尝试接受的答案失败后,我想建议一个风险较小的命令:
Update-Package <PackageName> -ProjectName <ProjectName> -Reinstall -IgnoreDependencies
For more info: http://blog.nuget.org/20121231/a-quick-tutorial-on-update-package-command.html
更多信息:http: //blog.nuget.org/20121231/a-quick-tutorial-on-update-package-command.html
回答by Craigology
Whilst attempting to reinstall packages solution wide, I encountered a dependency error (in spite of using the -ignoreDependenciesflag), and all the packages.configfiles for every project had been deleted. In VS2013, it seems that packages.configdoes not get flushed back to disk and re-added until all the upgraded dependencies/references are re-attached to the project.
在尝试重新安装包解决方案时,我遇到了依赖项错误(尽管使用了-ignoreDependencies标志),并且每个项目的所有packages.config文件都已被删除。在 VS2013 中,在所有升级的依赖项/引用重新附加到项目之前,packages.config似乎不会刷新回磁盘并重新添加。
In my case what worked was to upgrade each project one-at-a-time by adding the -ProjectNameprojectnameto the update-packagecommand. In this case the packages.configis updated as each project is upgraded.
在我的情况什么工作是通过将每个项目都有一个-AT-A-时间升级-ProjectName项目名的update-package命令。在这种情况下,packages.config会随着每个项目的升级而更新。
May not be practical for very large solutions but it seems a reasonable compromise to still take advantage of the automated upgrade for as many projects as possible and isolate the problematic ones without having every packages.configin your solution deleted on failure.
对于非常大的解决方案可能不切实际,但仍然利用尽可能多的项目的自动升级并隔离有问题的项目而不在失败时删除解决方案中的每个packages.config似乎是一个合理的妥协。
回答by Prabu Arumugam
With Visual Studio for Mac 2019, right-clicking the Packages folder shows 'Retarget' option in the menu. This resolved the retarget issue for all packages in the project that required retargeting. Looks like there was no NuGet Package Manager under Tools menu in Visual Studio for Mac (atleast in mine), so I couldn't launch Package Manager Console.
对于 Visual Studio for Mac 2019,右键单击 Packages 文件夹会在菜单中显示“重定向”选项。这解决了项目中所有需要重定向的包的重定向问题。看起来 Visual Studio for Mac 的工具菜单下没有 NuGet 包管理器(至少在我的),所以我无法启动包管理器控制台。


