我已经将目前针对VS2010中的。net 4.0的解决方案迁移到VS2012,现在我想将其重新定位到。net 4.5

我不确定的是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>

我还可以在包装中看到以下内容。项目的配置:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="5.0.0" targetFramework="net40" />
</packages>

所以我的问题是:

将所有NuGet包重新定位到。net 4.0的最佳实践是什么?


NuGet 2.1提供了一个特性,使这个过程简单得多:只需在包管理器控制台中执行update-package -重装-ignoreDependencies即可。

NuGet 2.0不能很好地处理应用程序的重新定位。为了改变你的包的目标框架,你必须卸载和重新安装包(记下你已经安装的包,这样你就可以重新安装它们)。

需要卸载和重装软件包的原因如下:

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


对于那些使用update-package -重装<packagename>命令有问题的人,可以考虑使用-ignoreDependencies标记来运行它,就像这样:

update-package -reinstall <packagename> -ignoreDependencies

这个标志将不影响你的包依赖项,否则它们可能会被更新,即使你最初想重新安装的包仍然保持它的版本相同。

更多信息请点击这里。


在尝试重新安装解决方案范围的包时,我遇到了一个依赖项错误(尽管使用了-ignoreDependencies标志),并且所有的包。每个项目的配置文件都已删除。在VS2013中,似乎包。在所有升级的依赖项/引用重新附加到项目之前,配置不会被刷新回磁盘并重新添加。

在我的例子中,有效的方法是通过在update-package命令中添加-ProjectName projectname来一次升级每个项目。在这种情况下,包裹。配置随着每个项目的升级而更新。

对于非常大的解决方案可能不太实际,但对于尽可能多的项目仍然利用自动化升级,并在没有每个包的情况下隔离有问题的项目,这似乎是一个合理的妥协。在失败时删除解决方案中的配置。


在尝试了公认的答案后,我想建议一个风险较小的命令:

Update-Package <PackageName> -ProjectName <ProjectName> -Reinstall -IgnoreDependencies

欲了解更多信息: http://blog.nuget.org/20121231/a-quick-tutorial-on-update-package-command.html


使用Visual Studio for Mac 2019,右键单击Packages文件夹,在菜单中显示“重定向”选项。这解决了项目中所有需要重新定位的包的重新定位问题。看起来在Mac的Visual Studio工具菜单下没有NuGet包管理器(至少在我的),所以我不能启动包管理器控制台。