如何重新安裝和更新套件How to reinstall and update packages

重新安裝套件的時機下方所述的許多情況中,Visual Studio 專案內套件的參考可能損毀。There are a number of situations, described below under When to Reinstall a Package, where references to a package might get broken within a Visual Studio project. 在這些情況下,解除安裝後重新安裝相同版本的套件,將會還原這些工作順序參考。In these cases, uninstalling and then reinstalling the same version of the package will restore those references to working order. 更新套件,只是表示安裝更新的版本,這通常會還原套件的工作順序。Updating a package simply means installing an updated version, which often restores a package to working order.

在 Visual Studio 中,套件管理員主控台會提供許多彈性的選項來更新及重新安裝套件。In Visual Studio, the Package Manager Console provides many flexible options for updating and reinstalling packages.

更新和重新安裝套件已完成,如下所示:Updating and reinstalling packages is accomplished as follows:

方法Method 更新Update 重新安裝Reinstall
套件管理員主控台 (如使用 Update-Package 中所述)Package Manager console (described in Using Update-Package) Update-Package 命令Update-Package command Update-Package -reinstall 命令Update-Package -reinstall command
套件管理員 UIPackage Manager UI 在 [更新] 索引標籤上,選取一或多個套件,然後選取 [更新] On the Updates tab, select one or more packages and select Update 在 [已安裝] 索引標籤上,選取套件,並記錄其名稱,然後選取 [解除安裝] 。On the Installed tab, select a package, record its name, then select Uninstall . 切換至 [瀏覽] 索引標籤,並搜尋套件名稱,再選取它,然後選取 [安裝] 。Switch to the Browse tab, search for the package name, select it, then select Install ).
nuget.exe CLInuget.exe CLI nuget update 命令nuget update command 針對所有套件,刪除套件資料夾,然後執行 nuget installFor all packages, delete the package folder, then run nuget install. 針對單一套件,刪除套件資料夾,然後使用 nuget install <id> 重新安裝相同套件。For a single package, delete the package folder and use nuget install <id> to reinstall the same one.

注意

若為 dotnet CLI,則不需要相同的程序。For the dotnet CLI, the equivalent procedure is not required. 在類似案例中,您可以使用 dotnet CLI 還原套件In a similar scenario, you can restore packages with the dotnet CLI.

本文內容:In this article:

重新安裝套件的時機When to Reinstall a Package

  1. 套件還原之後參考損毀 :如果您已開啟專案並還原 NuGet 套件,但仍看到損毀參考,請嘗試重新安裝所有這些套件。Broken references after package restore : If you've opened a project and restored NuGet packages, but still see broken references, try reinstalling each of those packages.
  2. 專案因已刪除檔案而中斷 :NuGet 不會防止您移除已從套件新增的項目,因此很容易不慎修改從套件安裝的內容,並中斷您的專案。Project is broken due to deleted files : NuGet does not prevent you from removing items added from packages, so it's easy to inadvertently modify contents installed from a package and break your project. 若要還原專案,請重新安裝受影響的套件。To restore the project, reinstall the affected packages.
  3. 套件更新中斷了專案 :如果套件的更新會中斷專案,失敗的原因通常是也曾經更新的相依性套件所造成。Package update broke the project : If an update to a package breaks a project, the failure is generally caused by a dependency package which may have also been updated. 若要還原相依性的狀態,請重新安裝該特定套件。To restore the state of the dependency, reinstall that specific package.
  4. 保護重設目標或升級 :已對專案重設目標或升級時,以及套件因目標架構變更而需要重新安裝時,這可能十分好用。Project retargeting or upgrade : This can be useful when a project has been retargeted or upgraded and if the package requires reinstallation due to the change in target framework. 在這類情況下,NuGet 會在專案重定目標之後立即顯示建置錯誤,而後續的建置警告可讓您知道可能需要重新安裝套件。NuGet shows a build error in such cases immediately after project retargeting, and subsequent build warnings let you know that the package may need to be reinstalled. 針對專案升級,NuGet 會在專案升級記錄中顯示錯誤。For project upgrade, NuGet shows an error in the Project Upgrade Log.
  5. 在開發套件期間重新安裝套件 :套件作者通常需要重新安裝他們所開發以測試該行為之相同版本的套件。Reinstalling a package during its development : Package authors often need to reinstall the same version of package they're developing to test the behavior. Install-Package 命令未提供選項來強制重新安裝,因此請改用 Update-Package -reinstallThe Install-Package command does not provide an option to force a reinstall, so use Update-Package -reinstall instead.

限制升級版本Constraining upgrade versions

重新安裝或更新套件預設「一律」 會安裝套件來源中可用的最新版本。By default, reinstalling or updating a package always installs the latest version available from the package source.

不過,在使用 packages.config 管理格式的專案中,您可以特別限制版本範圍。In projects using the packages.config management format, however, you can specifically constrain the version range. 例如,如果您知道您的應用程式僅適用於 1.x 版的套件,而不是 2.0 和更新版本 (可能是套件 API 中的主要變更所造成),則會想要限制 1.x 版的升級。For example, if you know that your application works only with version 1.x of a package but not 2.0 and above, perhaps due to a major change in the package API, then you'd want to constrain upgrades to 1.x versions. 這可避免破壞應用程式的意外更新。This prevents accidental updates that would break the application.

若要設定條件約束,請在文字編輯器中開啟 packages.config,並找到有問題的相依性,然後使用版本範圍新增 allowedVersions 屬性。To set a constraint, open packages.config in a text editor, locate the dependency in question, and add the allowedVersions attribute with a version range. 例如,若要限制 1.x 版的更新,請將 allowedVersions 設定為 [1,2)For example, to constrain updates to version 1.x, set allowedVersions to [1,2):

<?xml version="1.0" encoding="utf-8"?>
<packages>
    <package id="ExamplePackage" version="1.1.0" allowedVersions="[1,2)" />

    <!-- ... -->
</packages>

在所有情況下,都請使用套件版本控制中所述的標記法。In all cases, use the notation described in Package versioning.

使用 Update-PackageUsing Update-Package

請注意下面所述的 > [NuGet 套件管理員] > [套件管理員主控台] )。Being mindful of the Considerations described below, you can easily reinstall any package using the Update-Package command in the Visual Studio Package Manager Console ( Tools > NuGet Package Manager > Package Manager Console ).

Update-Package -Id <package_name> –reinstall

使用此命令,會比移除套件後嘗試使用相同的版本在 NuGet 資源庫中找到相同的套件更為簡單。Using this command is much easier than removing a package and then trying to locate the same package in the NuGet gallery with the same version. 請注意,-Id 是選擇性參數。Note that the -Id switch is optional.

沒有 -reinstall 的相同命令會將套件更新為較新版本 (適用時)。The same command without -reinstall updates a package to a newer version, if applicable. 如果專案中尚未安裝有問題的套件,則此命令會顯示錯誤;也就是說,Update-Package 不會直接安裝套件。The command gives an error if the package in question is not already installed in a project; that is, Update-Package does not install packages directly.

Update-Package <package_name>

Update-Package 預設會影響方案中的所有專案。By default, Update-Package affects all projects in a solution. 若要將動作限制為特定專案,請使用 -ProjectName 參數,並使用出現在方案總管中的專案名稱:To limit the action to a specific project, use the -ProjectName switch, using the name of the project as it appears in Solution Explorer:

# Reinstall the package in just MyProject
Update-Package <package_name> -ProjectName MyProject -reinstall

若要「更新」 專案中的所有套件 (或使用 -reinstall 重新安裝),請使用 -ProjectName,而不指定任何特定套件:To update all packages in a project (or reinstall using -reinstall), use -ProjectName without specifying any particular package:

Update-Package -ProjectName MyProject

若要更新方案中的所有套件,只需要使用 Update-Package 本身,而沒有其他引數或參數。To update all packages in a solution, just use Update-Package by itself with no other arguments or switches. 請小心使用此表單,因為它可能需要相當長的時間才能執行所有更新:Use this form carefully, because it can take considerable time to perform all the updates:

# Updates all packages in all projects in the solution
Update-Package 

使用 PackageReference 來更新專案或解決方案中的套件,一律會更新為最新版本的套件 (發行前套件除外)。Updating packages in a project or solution using PackageReference always updates to the latest version of the package (excluding pre-release packages). 如有需要,使用 packages.config 的專案可以限制下面限制升級版本中所述的更新版本。Projects that use packages.config can, if desired, limit update versions as described below in Constraining upgrade versions.

如需命令的完整詳細資料,請參閱 Update-Package 參考。For full details on the command, see the Update-Package reference.

考量Considerations

重新安裝套件時,下列可能會受到影響:The following may be affected when reinstalling a package:

  1. 根據專案目標架構重設目標來重新安裝套件Reinstalling packages according to project target framework retargeting

    • 在簡單的情況下,使用 Update-Package –reinstall <package_name> 重新安裝套件就能運作。In a simple case, just reinstalling a package using Update-Package –reinstall <package_name> works. 解除安裝針對舊目標架構所安裝的套件,並針對專案的目前目標架構安裝相同套件。A package that is installed against an old target framework gets uninstalled and the same package gets installed against the current target framework of the project.
    • 在某些情況下,可能會有不支援新目標架構的套件。In some cases, there may be a package that does not support the new target framework.
      • 如果套件支援可攜式類別庫 (PCL),並將專案的目標重設為套件不再支援的平台組合,則套件的參考會在重新安裝之後遺失。If a package supports portable class libraries (PCLs) and the project is retargeted to a combination of platforms no longer supported by the package, references to the package will be missing after reinstalling.
      • 這可能會出現您直接使用的套件,或作為相依性而安裝的套件。This can surface for packages you're using directly or for packages installed as dependencies. 您使用的套件可能會直接支援新的目標架構,而其相依性則否。It's possible for the package you're using directly to support the new target framework while its dependency does not.
      • 如果在重設應用程式的目標之後重新安裝套件導致組建或執行階段錯誤,您可能需要還原目標架構,或搜尋正確支援新目標架構的替代套件。If reinstalling packages after retargeting your application results in build or runtime errors, you may need to revert your target framework or search for alternative packages that properly support your new target framework.
  2. 專案重設目標或升級之後 packages.config 中所新增的 requireReinstallation 屬性requireReinstallation attribute added in packages.config after project retargeting or upgrade

    • 如果 NuGet 偵測到重設目標或升級專案已影響套件,則會將 packages.config 中的 requireReinstallation="true" 屬性新增至所有受影響的套件參考。If NuGet detects that packages were affected by retargeting or upgrading a project, it adds a requireReinstallation="true" attribute in packages.config to all affected package references. 因此,Visual Studio 中的每個後續組建都會引發這些套件的組建警告,讓您可以記得重新安裝它們。Because of this, each subsequent build in Visual Studio raises build warnings for those packages so you can remember to reinstall them.
  3. 使用相依性重新安裝套件Reinstalling packages with dependencies

    • Update-Package –reinstall 會重新安裝相同版本的原始套件,但除非提供特定版本條件約束,否則會安裝最新版本的相依性。Update-Package –reinstall reinstalls the same version of the original package, but installs the latest version of dependencies unless specific version constraints are provided. 這可讓您視需要僅更新相依性,以修正問題。This allows you to update only the dependencies as required to fix an issue. 不過,如果這會將相依性復原為舊版本,則您可以使用 Update-Package <dependency_name> 重新安裝該相依性,而不影響相依的套件。However, if this rolls a dependency back to an earlier version, you can use Update-Package <dependency_name> to reinstall that one dependency without affecting the dependent package.
    • Update-Package –reinstall <packageName> -ignoreDependencies 會重新安裝相同版本的原始套件,但不重新安裝相依性。Update-Package –reinstall <packageName> -ignoreDependencies reinstalls the same version of the original package but does not reinstall dependencies. 更新套件相依性可能導致中斷狀態時,請使用此項目Use this when updating package dependencies might result in a broken state
  4. 在涉及相依的版本時重新安裝套件Reinstalling packages when dependent versions are involved

    • 如上所述,重新安裝套件時不會變更與其相依之任何其他已安裝套件的版本。As explained above, reinstalling a package does not change versions of any other installed packages that depend on it. 接著,重新安裝相依性可能會中斷相依的套件。It's possible, then, that reinstalling a dependency could break the dependent package.