文章内容

2018/4/11 14:50:50,作 者: 黄兵

How to Add Multiple Projects in Visual Studio Code

Adding multiple projects in Visual Studio Code is a breeze. Not many people are aware that VS Code supports multiple projects.

在Visual Studio Code 中添加多个项目是一件轻而易举的事情。没有多少人知道VS Code支持多个项目。

Visual Studio Code doesn't support solution files, whereas Visual Studio does. In Visual Studio we use solution files to link multiple projects together, and at first I thought that this was going to be a deal breaker for Visual Studio Code - but it's not at all. Many people aren't aware that Visual Studio Code, although lightweight, is a powerful editor that can do most of the things you need.

Visual Studio Code 不支持解决方案文件,而Visual Studio则支持。在Visual Studio中,我们使用解决方案文件将多个项目链接在一起,起初我认为这将成为Visual Studio Code和Visual Studio的一个最主要区别,但现在看来不是这样的。许多人并不知道Visual Studio Code虽然很轻,却是一个功能强大的编辑器,可以完成大部分所需的工作。

Even though Visual Studio Code was probably meant as a directory based editor where you only work with files in a single directory, and many people only use it in this way, Visual Studio Code does support multiple projects. Visual Studio Code doesn't have the ability to add multiple projects via the menu items, but it can be done very easily and with minimal effort. Read along to see exactly how you can add multiple projects in Visual Studio Code.

尽管Visual Studio Code可能意味着基于目录的编辑器,只能在单个目录中处理文件,而且许多人只以这种方式使用它,但Visual Studio Code确实支持多个项目。Visual Studio Code不具备通过菜单项添加多个项目的功能,但它可以非常轻松地完成并且只需很少的工作。请仔细阅读,了解如何在Visual Studio代码中添加多个项目。

How To Use Multiple Projects with Visual Studio Code

如何在Visual Studio Code实现多项目

Using multiple projects with VS Code is quick and easy to do.

在VS Code中使用多个项目很快且容易。

Yes, Visual Studio Code doesn't support .sln files, but you can add project references between projects. This way, you maintain separate projects and each project is compiled separately, just like it works in Visual Studio.

是的,Visual Studio代码不支持.sln文件,但可以在项目之间添加项目引用。这样,您可以维护单独的项目,并且每个项目都是单独编译的,就像它在Visual Studio中一样。

Let me show you how it works.

让我告诉你它是如何工作的。

Step 1

Create a new directory that will house all your projects. Think of this as your solution directory.

创建一个新的目录,将容纳所有的项目。将此视为您的解决方案目录。

mkdir vs-code-multiple-projects

Step 2

Change into the directory, and create your first project with .NET Core's CLI tooling.

切换到目录,并使用.NET Core的CLI工具创建您的第一个项目。

cd vs-code-multiple-projects
dotnet new mvc -o HumanKode.Web

Step 3

Now, create the second project.

现在,创建第二个项目。

dotnet new classlib -o HumanKode.Extensions

Step 4

Open your project and add a reference. Change into the directory of the project to which you want to add a reference. 
In this instance, I want to add a reference to the class library project in my web project, so I'm going to change into the web project directory and run the add reference command from the web directory which will add a reference to the class library project. 

打开你的项目并添加一个引用。转到您要添加引用的项目的目录。
在这个例子中,我想添加一个对我的web项目中的类库项目的引用,所以我要更改为web项目目录并从web目录运行add reference命令,该命令将添加对类的引用。 

cd HumanKode.Web
dotnet add reference ../HumanKode.Extensions/HumanKode.Extensions.csproj

 

If you view the web csproj file, you will see a reference was added.

如果您查看Web csproj文件,您将看到添加了一个引用。

HumanKode.Web.csproj

<ItemGroup>
    <ProjectReference Include="..\HumanKode.Extensions\HumanKode.Extensions.csproj" />
</ItemGroup>

 

Conclusion

结论

Using this method, you can add as many projects as you want. 
Since using this method, I've started converting more of my older Visual Studio projects to Visual Studio Code projects using .NET core.

使用这种方法,您可以添加尽可能多的项目。
自从使用这种方法以来,我开始使用.NET Core将更多旧的Visual Studio项目转换为Visual Studio Code项目。

If you liked this post, stay tuned for the upcoming follow-up post where I'll show you how you can use Visual Studio Code with Entity Framework Core in a separate project.


原文地址:How to Add Multiple Projects in Visual Studio Code

分享到:

发表评论

评论列表