C# .NET标准类库无法加载文件或程序集Microsoft.Data.Sqlite
我正在尝试创建一个.NET标准类库来访问SQLite数据库。经过大量研究,我认为Microsoft.Data.Sqlite最适合 但是,当调用需要包的类库方法时,我会遇到以下异常: 无法加载文件或程序集“Microsoft.Data.Sqlite,版本=2.1.0.0,区域性=中性,PublicKeyToken=adb9793829ddae60”或其依赖项之一。系统找不到指定的文件 我使用.NET Framework 4.6.2在一个非常简单的控制台应用程序中隔离了该问题,该应用程序在.NET标准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标准类库 关于堆栈溢出的最接近的问题如下,但它没有解决我的问题:C# .NET标准类库无法加载文件或程序集Microsoft.Data.Sqlite,c#,.net,sqlite,.net-standard,C#,.net,Sqlite,.net Standard,我正在尝试创建一个.NET标准类库来访问SQLite数据库。经过大量研究,我认为Microsoft.Data.Sqlite最适合 但是,当调用需要包的类库方法时,我会遇到以下异常: 无法加载文件或程序集“Microsoft.Data.Sqlite,版本=2.1.0.0,区域性=中性,PublicKeyToken=adb9793829ddae60”或其依赖项之一。系统找不到指定的文件 我使用.NET Framework 4.6.2在一个非常简单的控制台应用程序中隔离了该问题,该应用程序在.NET标
这个问题的背景是,我正在尝试为Windows和Mac创建一个桌面应用程序。我希望创建一个.NET标准类库来处理数据访问,使用SQLite作为实际数据库。我愿意接受其他建议。这似乎是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个引用此类库的项目文件
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0" />
</ItemGroup>
</Project>
第一:
第二:
第三:
第一个项目没有找到您编写的Microsoft.Data.Sqlite.dll。
第二个项目将找到Microsoft.Data.Sqlite.dll,但缺少一些e_sqlite3.dll文件
第三个项目将运行良好。
这三个项目之间的区别:
Project1参考类库1
Project2引用ClassLibrary1和NuGet NewtonSoft.Json
Project3引用ClassLibrary1和NuGet NewtonSoft.Json、Microsoft.Dara.Sqlite
Json-只要引用了NuGet包并使用PackageReference,就可以使用任何NuGet包
VS Bug
如果未通过PackageReference向任何NuGet包添加引用,则项目将不包括所包含项目中的任何NuGet包引用
Microsoft.Data.Sqlite
仅当应用程序项目直接引用包时,才会复制所需的文件x64/x86 e_sqlite3.dll文件 我只是通过明确添加
<PackageReference Include="Microsoft.Data.Sqlite" Version="$(MicrosoftDataSqliteVersion)" />
指向引用了原始项目的基础项目。Microsoft.Data.Sqlite现在得到了正确的引用Microsoft.Data.Sqlite.Core支持netstandard2.0我尝试添加Microsoft.Data.Sqlite.Core,并用Microsoft.Data.Sqlite.Core替换Microsoft.Data.Sqlite.Core,我得到了相同的异常。我猜您使用packages.config文件管理包。您应该在项目文件中使用PackageReference,如下所示
<?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>
<PackageReference Include="Microsoft.Data.Sqlite" Version="$(MicrosoftDataSqliteVersion)" />