如何处理 C# Visual Studio 解决方案中的引用?

How to handle references in a C# Visual Studio solution?

假设我有一个 visual studio 2019 解决方案,其中包含两个用 C# 编写的项目,其中一个是控制台应用程序,另一个是 class 库。

控制台应用程序现在引用了 class 库,例如,它本身需要 entity framework。

问题是,有没有办法将 entity framework 包含在 class 库中,以便控制台应用程序只需要引用 class 库而不是entity framework 也是吗?

is there a way on how to include the entity framework within the class library so that the console app only needs a reference to the class library and not to the entity framework as well?

  • 是的,有办法做到这一点。如果您根本没有在控制台应用程序中使用 entity framework,则不需要它在该项目中添加引用。

在控制台应用程序和 class 库中添加引用的步骤:

  1. 使用右键单击 class 库项目中的引用时可用的 Manage NuGet Packages.. 选项将所需的 NuGet 包添加到 class 库项目。

  2. 现在使用 Add Reference -> Projects -> [= 添加 class 库 项目引用 到控制台应用程序13=].

这样您就不需要向控制台应用程序添加依赖程序集。每当您在 class 库项目中更新时,它都会在重建解决方案后反映到控制台应用程序。

只需右键单击 class 库项目,然后从菜单管理 NuGet 包中 select。从“浏览”选项卡中搜索 EntityFramework。确保 select 版本正确。