MSBuild nuget包:要添加到.csproj文件中的内容?

时间:2018-04-05 18:08:37

标签: c# msbuild nuget

我正在使用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>

但是,当我运行msbuild myproj.csproj时,我收到错误The type or namespace SQLite does not exist...

我还需要添加其他内容吗?

这里是完整的文件:

<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>

提前致谢。

1 个答案:

答案 0 :(得分:1)

这是csproj的旧风格:

<Project DefaultTargets = "All" 

的xmlns =&#34; HTTP://schemas.microsoft.com/developer/msbuild/2003"&GT;

我不认为<PackageReference>在那里有效。您需要一个新的.NET-Core风格的项目文件,您可以从一开始就知道它:

<Project Sdk="Microsoft.NET.Sdk">

您可以使用.NET Core SDK(dotnet build等)或MSBuild构建它,虽然我认为您需要安装.NET Core SDK才能使用它甚至与MSBuild一起工作。 (我个人建议使用dotnet build等而不是调用MSBuild,但这取决于你。无论哪种方式,你绝对不需要IDE。)

在旧式项目文件中添加NuGet依赖项要更加繁琐,因为它基本上涉及添加对各个DLL的引用,以及使DLL可用的额外文件。我肯定建议使用新式项目文件。