C# MSBuild nuget包:要向.csproj文件添加什么?

C# MSBuild nuget包:要向.csproj文件添加什么?,c#,msbuild,nuget,C#,Msbuild,Nuget,我正在通过终端使用MSBuild编译和运行一个C#项目,它工作正常 现在,我需要向我的项目中添加一些nuget包(即SQLite) 这是我在myproj.csproj中添加的内容: <!-- NuGet Packages --> <ItemGroup> <PackageReference Include="EntityFramework" Version="6.0.0" /> <PackageReference Include="Sy

我正在通过终端使用MSBuild编译和运行一个C#项目,它工作正常

现在,我需要向我的项目中添加一些nuget包(即SQLite)

这是我在myproj.csproj中添加的内容:

<!-- NuGet Packages -->
  <ItemGroup>
    <PackageReference Include="EntityFramework" Version="6.0.0" />
    <PackageReference Include="System.Data.SQLite" Version="1.0.108.0" />
    <PackageReference Include="System.Data.SQLite.Core" Version="1.0.108.0" />
    <PackageReference Include="System.Data.SQLite.EF6" Version="1.0.108.0" />
    <PackageReference Include="System.Data.SQLite.Linq" Version="1.0.108.0" />
  </ItemGroup>

提前感谢。

这是csproj的老风格:


我认为
在那里是无效的。您需要一个新的.NET Core样式的项目文件,从一开始就可以看出:


您可以使用.NET Core SDK(
dotnet build
etc)或MSBuild来构建它,尽管我认为您需要安装.NET Core SDK才能使其与MSBuild一起工作。(我个人建议使用
dotnetbuild
etc而不是调用MSBuild,但这取决于您。不管怎样,您肯定不需要IDE。)


在旧式项目文件中添加NuGet依赖项要复杂得多,因为它基本上涉及到添加对单个DLL的引用,以及使DLL可用的额外文件。我绝对建议改用新样式的项目文件。

您是否正在执行
numget restore
来下载软件包?@p.Brian.Mackey我没有使用IDE,只使用VSCode。@RonBeyer我尝试了
msbuild/t:restore
,但我得到了目标“restore”的错误
不存在
。您使用的msbuild版本是什么?Visual Studio社区版是免费的。VS代码非常适合处理前端代码。csproj文件不太好。哦,我明白了。我在创建.csproj时遵循了教程,不知道有一种新的方法。我听从了你的建议,选择了新的方式,我的项目现在已经成功构建。但是,它将所有内容构建到一个
myproj.dll
文件中。我需要构建一个
server.exe
,一个
client.exe
和一个
common.dll
。我如何才能将原始.csproj文件中的构建目标添加到这个新文件中?@Tirafesi:如果你有三个项目,它不会将所有内容都构建到一个DLL中,这听起来是你应该拥有的。如果您有三个代码库,那么自然的方法是将其组织为三个项目。为什么要尝试将其构建为单个项目文件?你知道,你完全正确!我想制作一个单一的项目文件,这样我就可以同时编译所有的东西,但我想这不是应该做的事情:非常感谢@Tirafesi:如果您愿意,您可以轻松地同时编译所有内容-只需创建一个解决方案文件。将所有项目添加到解决方案中,然后构建解决方案。(在这方面,至少有一些Visual Studio的经验将有助于您理解许多项目系统设计所反映的内容。这并不意味着您需要继续使用它,但获得一些使用它的经验将是有益的。)
<Project DefaultTargets = "All" 
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- Server -->
  <PropertyGroup>
    <ServerAssemblyName>server.exe</ServerAssemblyName>
    <ServerSrcPath>src/server/</ServerSrcPath>
  </PropertyGroup>

  <!-- Client -->
  <PropertyGroup>
    <ClientAssemblyName>client.exe</ClientAssemblyName>
    <ClientSrcPath>src/client/</ClientSrcPath>
  </PropertyGroup>

  <!-- Common -->
  <PropertyGroup>
    <CommonAssemblyName>common.dll</CommonAssemblyName>
    <CommonSrcPath>src/common/</CommonSrcPath>
    <OutputPath>bin/</OutputPath>
  </PropertyGroup>

  <!-- Server -->
  <ItemGroup>
    <ServerCompile Include="$(ServerSrcPath)Server.cs" />
    <ServerCompile Include="$(ServerSrcPath)Diginote.cs" />
    <ServerCompile Include="$(ServerSrcPath)DB.cs" />
  </ItemGroup>

  <!-- Client -->
  <ItemGroup>
    <ClientCompile Include="$(ClientSrcPath)Client.cs" />
    <ClientCompile Include="$(ClientSrcPath)cli/CLI.cs" />
    <ClientCompile Include="$(ClientSrcPath)cli/Menu.cs" />
    <ClientCompile Include="$(ClientSrcPath)cli/MainMenu.cs" />
  </ItemGroup>

  <!-- Common -->
  <ItemGroup>
    <CommonCompile Include="$(CommonSrcPath)Common.cs" />
  </ItemGroup>

  <!-- NuGet Packages -->
  <ItemGroup>
    <PackageReference Include="EntityFramework" Version="6.0.0" />
    <PackageReference Include="System.Data.SQLite" Version="1.0.108.0" />
    <PackageReference Include="System.Data.SQLite.Core" Version="1.0.108.0" />
    <PackageReference Include="System.Data.SQLite.EF6" Version="1.0.108.0" />
    <PackageReference Include="System.Data.SQLite.Linq" Version="1.0.108.0" />
  </ItemGroup>

  <!-- Server -->
  <Target Name="Server" DependsOnTargets="Common" Inputs="@(ServerCompile)" Outputs="$(OutputPath)$(ServerAssemblyName)">
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
    <Csc Sources="@(ServerCompile)" References="$(OutputPath)$(CommonAssemblyName)" OutputAssembly="$(OutputPath)$(ServerAssemblyName)"/>
  </Target>

  <!-- Client -->
  <Target Name="Client" DependsOnTargets="Common" Inputs="@(ClientCompile)" Outputs="$(OutputPath)$(ClientAssemblyName)">
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
    <Csc Sources="@(ClientCompile)" References="$(OutputPath)$(CommonAssemblyName)" OutputAssembly="$(OutputPath)$(ClientAssemblyName)"/>
  </Target>

  <!-- Common -->
  <Target Name="Common" Inputs="@(CommonCompile)" Outputs="$(OutputPath)$(CommonAssemblyName)">
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
    <Csc Sources="@(CommonCompile)" OutputAssembly="$(OutputPath)$(CommonAssemblyName)" TargetType="Library"/>
  </Target>

  <Target Name="Clean">
    <Delete Files="$(OutputPath)$(ServerAssemblyName)" />
    <Delete Files="$(OutputPath)$(ClientAssemblyName)" />
    <Delete Files="$(OutputPath)$(CommonAssemblyName)" />
  </Target>

  <Target Name="All" DependsOnTargets="Server;Client;Common" />

  <Target Name="Rebuild" DependsOnTargets="Clean;All" />
</Project>