NuGet use Package Manager to install and manage packages in Visual Studio

https://docs.microsoft.com/zh-cn/nuget/consume-packages/install-use-packages-visual-studio

 

With Windows version of Visual Studio NuGet package manager UI, easy to install, uninstall, and update projects and solutions in NuGet package. To learn more about Visual Studio for Mac experience, please see the project included NuGet packageVisual Studio Code does not contain a package manager UI.

 Remark

If Visual Studio 2015 is missing NuGet Package Manager, select "Tools"> "expanded and updated ..." and search for "NuGet Package Manager" extension. If you can not use the Extension Installer in Visual Studio, please contact the  https://dist.nuget.org/index.html  download extensions.

Starting Visual Studio 2017, NuGet and NuGet Package Manager is installed automatically with any .NET-related workloads. By selecting the "single component"> "code means" in the Visual Studio Setup> "the NuGet Package Manager" option, it may be installed separately.

Find and install the package

  1. In the "Solution Explorer", right-click "quote" or an item, then select "Manage NuGet package ...."

    "NuGet Management Pack" menu option

  2. "Browse" tab by popularity display the currently selected source package (see package source ). Use the top left of the search box to search for a specific package. Select a package from the list to display its information, this will also enable the "Install" button and select the version drop-down list.

    "Management NuGet package" dialog "Browse" tab

  3. Select the desired version from the drop-down list, then select "Install." Visual Studio will then install the package and its dependencies to the project. You may be asked to accept the license terms. After the installation is complete, add the package will be displayed on the tab "Installed." Package is also listed in the Solution Explorer "reference" node, indicating that you can use the  using statement to refer to them in the project.

    Solution Explorer in the "References"

 prompt

To include in the search pre-release versions, and provide pre-release version in the drop-down list, select the "Include pre-release version" option.

Uninstall package

  1. In the "Solution Explorer", right-click the "References" or desired item, then select "Manage NuGet package ...."

  2. Select "Install" tab.

  3. Select the package to be uninstalled (if necessary, use the search filter the list) and select "Uninstall."

    Uninstall package

  4. Please note that when you uninstall the package, "including pre-release version" and "packet source" control invalid.

Update package

  1. In the "Solution Explorer", right-click the "References" or desired item, then select "Manage NuGet package ...." (Project site, right-click the "Bin" folder.)

  2. Select "Update" tab to view the selected package contains the source packages available updates. Select "contains pre-release version" to include pre-release version of the package in the update list.

  3. Select the package you want to update, select the desired version from the drop-down list on the right, then select "Update."

    Update package

  4. 对于某些包,“更新” 按钮处于禁用状态,并显示一条消息,指示它是“由 SDK 隐式引用”(或“AutoReferenced”)。 此消息表明该包是较大框架或 SDK 的一部分,不能单独更新。 (此类包在内部标有 <IsImplicitlyDefined>True</IsImplicitlyDefined>。)例如,Microsoft.NETCore.App 是 .NET Core SDK 的一部分,并且包版本与应用程序使用的运行时框架的版本不同。 需要更新 .NET Core 安装以获取新版本的 ASP.NET Core 和 .NET Core 运行时。 请参阅本文档,详细了解 .NET Core 元包和版本控制。 这适用于以下常用包:

    • Microsoft.AspNetCore.All
    • Microsoft.AspNetCore.App
    • Microsoft.NETCore.App
    • NETStandard.Library

    Example labeled "implicit reference" or "AutoReferenced" package

  5. 若要将多个包更新到其最新版本,请在列表中选中它们,然后选择列表上方的“更新” 按钮。

  6. 还可以从“已安装” 选项卡更新单个包。在这种情况下,包的详细信息包括版本选择器(受“包含预发布版本” 选项的约束)和“更新” 按钮。

管理解决方案的包

管理解决方案的包是同时处理多个项目的便捷方式。

  1. 选择“工具”>“NuGet 包管理器”>“管理解决方案的 NuGet 包...”菜单命令,或右键单击解决方案,然后选择“管理 NuGet 包...” :

    Management solutions NuGet package

  2. 管理解决方案的包时,UI 让你可以选择受操作影响的项目:

    Project selector when the packet management solutions

“合并”选项卡

开发人员通常认为,在同一解决方案的不同项目中使用相同 NuGet 包的不同版本的做法不好。 当你选择管理解决方案的包时,包管理器 UI 提供了一个“合并” 选项卡,让你可以轻松查看解决方案中不同项目使用的具有不同版本号的包:

Package Manager UI "Merge" tab

在本例中,ClassLibrary1 项目使用的是 EntityFramework 6.2.0,而 ConsoleApp1 使用的是 EntityFramework 6.1.0。 若要合并包版本,请执行以下操作:

  • 在项目列表中选择要更新的项目。
  • 选择要在“版本” 控件中的所有项目中使用的版本,例如 EntityFramework 6.2.0。
  • 选择“安装”按钮 。

包管理器将选定的包版本安装到所有选定的项目中,之后包不再显示在“合并”选项卡上 。

包源

若要更改 Visual Studio 从中获取包的源,请从源选择器中选择一个源:

Package manager UI package source selector

管理包源:

  1. 在下面的包管理器 UI 中选择“设置” 图标,或使用“工具”>“选项” 命令并滚动到“NuGet 包管理器” :

    Package Manager UI "Settings" icon

  2. 选择“包源”节点 :

    "Package source" option

  3. 要添加源,请选择“+” ,编辑名称,在“源” 控件中输入 URL 或路径,然后选择“更新” 。 选择器下拉列表中现在显示源。

  4. 若要更改包源,请选中它,在“名称” 和“源” 框中进行编辑,然后选择“更新” 。

  5. 若要禁用包源,请清除列表中名称左侧的框。

  6. 若要删除包源,请选中它,然后选择“X” 按钮。

  7. 使用向上和向下箭头按钮不会更改包源的优先级顺序。 Visual Studio 会忽略包源的顺序,使用来自任何首先响应请求的源的包。有关详细信息,请参阅包源

 提示

如果在删除某个包源后,该包源重新出现,则它可能会列在计算机级或用户级 NuGet.Config 文件中。 有关这些文件的位置,请参阅常见 NuGet 配置,然后通过手动编辑文件或使用 nuget 源命令删除源。

包管理器“选项”控件

选择包后,包管理器 UI 会在版本选择器下方显示一个可扩展的“选项”小控件(此处显示为折叠和展开) 。 请注意,对于某些项目类型,仅提供“显示预览窗口”选项 。

Package Manager Options

以下各节介绍了这些选项。

显示预览窗口

选中此选项后,模式窗口将显示安装包之前所选包的依赖项:

Examples of the "Preview" box

安装与更新选项

(并非适用于所有项目类型。)

“依赖项行为” 用于配置 NuGet 如何决定要安装哪些版本的依赖包:

  • “忽略依赖项” 会跳过安装任何依赖项,这通常会破坏正在安装的软件包。
  • 如果选择“最低” [默认选项],则安装具有可满足主要选定包要求的最小版本号的依赖项。
  • 如果选择“最高版本的修补程序” ,则安装的版本的主要版本号和次要版本号相同,但修补程序版本号最高。 例如,如果指定版本 1.2.2,则会安装以 1.2 开头的最高版本
  • 如果选择“最高次要版本” ,则安装的版本的主要版本号相同,但次要版本号和修补程序版本号最高。 如果指定版本 1.2.2,则会安装以 1 开头的最高版本
  • 选择“最高” 可安装包的最高可用版本。

“文件冲突操作” 指定 NuGet 应如何处理项目或本地计算机中已存在的包:

  • “提示” 指示 NuGet 询问是保留还是覆盖现有包。
  • “全部忽略” 指示 NuGet 跳过覆盖任何现有包。
  • “全部覆盖” 指示 NuGet 覆盖任何现有包。

卸载选项

(并非适用于所有项目类型。)

“删除依赖项” :如果选中,则删除任何依赖包(如果它们未在项目中的其他位置引用)。

“在存在依赖项时仍强制卸载” :选中后,即使在项目中仍然引用了该包,也会卸载它。 此选项通常与“删除依赖项” 一起选中,用于删除包及其安装的任何依赖项。 不过,使用此选项可能会导致项目中的引用中断。 在这种情况下,可能需要重新安装其他包

Guess you like

Origin www.cnblogs.com/wfy680/p/11967104.html