为了账号安全,请及时绑定邮箱和手机立即绑定

.NET Standard 类库无法加载文件或程序集 Microsoft.Data.Sqlite

.NET Standard 类库无法加载文件或程序集 Microsoft.Data.Sqlite

C#
泛舟湖上清波郎朗 2022-01-09 15:22:31
我正在尝试创建一个访问 SQLite 数据库的 .NET Standard 类库。经过大量研究,我认为 Microsoft.Data.Sqlite 效果最好。但是在调用需要包的类库方法时出现以下异常:无法加载文件或程序集 'Microsoft.Data.Sqlite, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' 或其依赖项之一。该系统找不到指定的文件。我已经在一个非常简单的控制台应用程序中隔离了这个问题,该应用程序使用 .NET Framework 4.6.2 调用 .NET Standard 2.0 类库中的方法。我使用 NuGet 包管理器将 Microsoft.Data.Sqlite 2.1.0 添加到类库中。我在 Windows 10 上使用 Visual Studio Community 2017(版本 15.8.3)。在创建直接使用 Microsoft.Data.Sqlite 的 .NET Core 2.1 控制台应用程序时我没有遇到任何问题(也就是说,没有单独的 .NET Standard 类库)。这个问题的上下文是我正在尝试为 Windows 和 Mac 创建一个桌面应用程序。我希望创建一个 .NET Standard 类库来处理数据访问,使用 SQLite 作为实际数据库。我愿意接受其他建议。
查看完整描述

2 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

这似乎是 Visual Studio和Microsoft.Data.Sqlite 包中的错误


我有一个类库


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


  <PropertyGroup>

    <TargetFramework>netstandard2.0</TargetFramework>

  </PropertyGroup>


  <ItemGroup>

    <PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0" />

  </ItemGroup>


</Project>

和3个引用这个类库的项目文件


第一的:


<?xml version="1.0" encoding="utf-8"?>

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

  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

  <PropertyGroup>

    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>

    <ProjectGuid>{8DA91D88-9D79-4F06-BEA4-00C408542D4F}</ProjectGuid>

    <OutputType>Exe</OutputType>

    <RootNamespace>ConsoleApp3</RootNamespace>

    <AssemblyName>ConsoleApp3</AssemblyName>

    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>

    <FileAlignment>512</FileAlignment>

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

    <Deterministic>true</Deterministic>

  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

    <PlatformTarget>AnyCPU</PlatformTarget>

    <DebugSymbols>true</DebugSymbols>

    <DebugType>full</DebugType>

    <Optimize>false</Optimize>

    <OutputPath>bin\Debug\</OutputPath>

    <DefineConstants>DEBUG;TRACE</DefineConstants>

    <ErrorReport>prompt</ErrorReport>

    <WarningLevel>4</WarningLevel>

  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

    <PlatformTarget>AnyCPU</PlatformTarget>

    <DebugType>pdbonly</DebugType>

    <Optimize>true</Optimize>

    <OutputPath>bin\Release\</OutputPath>

    <DefineConstants>TRACE</DefineConstants>

    <ErrorReport>prompt</ErrorReport>

    <WarningLevel>4</WarningLevel>

  </PropertyGroup>

  <ItemGroup>

    <Reference Include="System" />

    <Reference Include="System.Core" />

    <Reference Include="System.Xml.Linq" />

    <Reference Include="System.Data.DataSetExtensions" />

    <Reference Include="Microsoft.CSharp" />

    <Reference Include="System.Data" />

    <Reference Include="System.Net.Http" />

    <Reference Include="System.Xml" />

  </ItemGroup>

  <ItemGroup>

    <Compile Include="Program.cs" />

    <Compile Include="Properties\AssemblyInfo.cs" />

  </ItemGroup>

  <ItemGroup>

    <None Include="App.config" />

  </ItemGroup>

  <ItemGroup>

    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj">

      <Project>{eb6c148b-91a1-4f10-8703-2b93af5c4de6}</Project>

      <Name>ClassLibrary1</Name>

    </ProjectReference>

  </ItemGroup>

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

</Project>

第二:


<?xml version="1.0" encoding="utf-8"?>

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

  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

  <PropertyGroup>

    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>

    <ProjectGuid>{292E456C-AD14-452A-8D12-409157D79673}</ProjectGuid>

    <OutputType>Exe</OutputType>

    <RootNamespace>ConsoleApp2</RootNamespace>

    <AssemblyName>ConsoleApp2</AssemblyName>

    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>

    <FileAlignment>512</FileAlignment>

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

    <Deterministic>true</Deterministic>

  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

    <PlatformTarget>AnyCPU</PlatformTarget>

    <DebugSymbols>true</DebugSymbols>

    <DebugType>full</DebugType>

    <Optimize>false</Optimize>

    <OutputPath>bin\Debug\</OutputPath>

    <DefineConstants>DEBUG;TRACE</DefineConstants>

    <ErrorReport>prompt</ErrorReport>

    <WarningLevel>4</WarningLevel>

  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

    <PlatformTarget>AnyCPU</PlatformTarget>

    <DebugType>pdbonly</DebugType>

    <Optimize>true</Optimize>

    <OutputPath>bin\Release\</OutputPath>

    <DefineConstants>TRACE</DefineConstants>

    <ErrorReport>prompt</ErrorReport>

    <WarningLevel>4</WarningLevel>

  </PropertyGroup>

  <ItemGroup>

    <Reference Include="System" />

    <Reference Include="System.Core" />

    <Reference Include="System.Xml.Linq" />

    <Reference Include="System.Data.DataSetExtensions" />

    <Reference Include="Microsoft.CSharp" />

    <Reference Include="System.Data" />

    <Reference Include="System.Net.Http" />

    <Reference Include="System.Xml" />

  </ItemGroup>

  <ItemGroup>

    <Compile Include="Program.cs" />

    <Compile Include="Properties\AssemblyInfo.cs" />

  </ItemGroup>

  <ItemGroup>

    <None Include="App.config" />

  </ItemGroup>

  <ItemGroup>

    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj">

      <Project>{eb6c148b-91a1-4f10-8703-2b93af5c4de6}</Project>

      <Name>ClassLibrary1</Name>

    </ProjectReference>

  </ItemGroup>

  <ItemGroup>

    <PackageReference Include="Newtonsoft.Json">

      <Version>11.0.2</Version>

    </PackageReference>

  </ItemGroup>

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

</Project>

第三:


<?xml version="1.0" encoding="utf-8"?>

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

  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

  <PropertyGroup>

    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>

    <ProjectGuid>{FBE757BF-025E-4167-90D0-EAD60E50C7EB}</ProjectGuid>

    <OutputType>Exe</OutputType>

    <RootNamespace>ConsoleApp1</RootNamespace>

    <AssemblyName>ConsoleApp1</AssemblyName>

    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>

    <FileAlignment>512</FileAlignment>

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

    <Deterministic>true</Deterministic>

  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

    <PlatformTarget>AnyCPU</PlatformTarget>

    <DebugSymbols>true</DebugSymbols>

    <DebugType>full</DebugType>

    <Optimize>false</Optimize>

    <OutputPath>bin\Debug\</OutputPath>

    <DefineConstants>DEBUG;TRACE</DefineConstants>

    <ErrorReport>prompt</ErrorReport>

    <WarningLevel>4</WarningLevel>

  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

    <PlatformTarget>AnyCPU</PlatformTarget>

    <DebugType>pdbonly</DebugType>

    <Optimize>true</Optimize>

    <OutputPath>bin\Release\</OutputPath>

    <DefineConstants>TRACE</DefineConstants>

    <ErrorReport>prompt</ErrorReport>

    <WarningLevel>4</WarningLevel>

  </PropertyGroup>

  <ItemGroup>

    <Reference Include="System" />

    <Reference Include="System.Core" />

    <Reference Include="System.Xml.Linq" />

    <Reference Include="System.Data.DataSetExtensions" />

    <Reference Include="Microsoft.CSharp" />

    <Reference Include="System.Data" />

    <Reference Include="System.Net.Http" />

    <Reference Include="System.Xml" />

  </ItemGroup>

  <ItemGroup>

    <Compile Include="Program.cs" />

    <Compile Include="Properties\AssemblyInfo.cs" />

  </ItemGroup>

  <ItemGroup>

    <None Include="App.config" />

  </ItemGroup>

  <ItemGroup>

    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj">

      <Project>{eb6c148b-91a1-4f10-8703-2b93af5c4de6}</Project>

      <Name>ClassLibrary1</Name>

    </ProjectReference>

  </ItemGroup>

  <ItemGroup>

    <PackageReference Include="Microsoft.Data.Sqlite">

      <Version>2.1.0</Version>

    </PackageReference>

    <PackageReference Include="Newtonsoft.Json">

      <Version>11.0.2</Version>

    </PackageReference>

  </ItemGroup>

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

</Project>

  • 如您所写,第一个项目没有找到Microsoft.Data.Sqlite.dll

  • 第二个项目将找到Microsoft.Data.Sqlite.dll但是缺少一些e_sqlite3.dll文件

  • 第三个项目将运行良好。

这三个项目的区别:

  • Project1引用ClassLibrary1

  • Project2引用ClassLibrary1和 NuGet NewtonSoft.Json

  • Project3引用ClassLibrary1和 NuGet NewtonSoft.JsonMicrosoft.Dara.Sqlite

NewtonSoft.Json - 只要您引用了 NuGet 包并使用PackageReference ,就可以使用任何 NuGet 包)。

VS 错误

如果您没有通过 PackageReference 添加对任何 NuGet 包的引用,则项目将不包含来自所包含项目的任何 NuGet 包引用

Microsoft.Data.Sqlite

需要的文件 x64/x86 e_sqlite3.dll 文件只有在应用项目直接引用包时才会被复制。


查看完整回答
反对 回复 2022-01-09
?
慕村225694

TA贡献1880条经验 获得超4个赞

我刚刚通过明确添加它来工作

<PackageReference Include="Microsoft.Data.Sqlite" Version="$(MicrosoftDataSqliteVersion)" />

到对您的原始项目有引用的基础项目。Microsoft.Data.Sqlite 现在被正确引用


查看完整回答
反对 回复 2022-01-09
  • 2 回答
  • 0 关注
  • 657 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号