繁体   English   中英

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

[英]MSBuild nuget package: what to add to .csproj file?

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

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

这是我添加到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>

提前致谢。

这是 csproj 的旧样式:

<Project DefaultTargets = "All" 
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

我不认为<PackageReference>在那里有效。 您需要一个新的 .NET Core 样式的项目文件,您可以从它的开头看出:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM