从System.Data.SQLite.Core 1.0.109 开始,您不需要自己编译任何东西,因为原生SQLite.Interop.dll 文件包含在适用于所有平台(Linux、macOS 和 Windows)的 NuGet 包中。请注意,虽然dll 扩展名用于所有平台,但这些文件实际上是原生动态库(在 macOS 上通常后缀为 dylib,在 Linux 上通常后缀为 so)。
$ find ~/.nuget/packages/system.data.sqlite.core/1.0.111/runtimes -name SQLite.Interop.dll -print0 | xargs -0 file
…/runtimes/linux-x64/native/netstandard2.0/SQLite.Interop.dll: ELF 64-bit LSB pie executable x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=96ce4120b31bad7d95f7b9ccf7c4bbb7717ae0b1, with debug_info, not stripped
…/runtimes/osx-x64/native/netstandard2.0/SQLite.Interop.dll: Mach-O 64-bit dynamically linked shared library x86_64
…/runtimes/win-x86/native/netstandard2.0/SQLite.Interop.dll: PE32 executable (DLL) (GUI) Intel 80386, for MS Windows
…/runtimes/win-x64/native/netstandard2.0/SQLite.Interop.dll: PE32+ executable (DLL) (GUI) x86-64, for MS Windows
不幸的是,负责将本机动态库复制到输出目录的 MSBuild 目标仅适用于 Windows。这可能是因为该软件包的作者假设 .NET Framework 仅在 Windows 上运行,这要归功于 Mono。另外,如果你想看看,CopySQLiteInteropFiles 目标可以在~/.nuget/packages/system.data.sqlite.core/{version}/build/net4*/System.Data.SQLite.Core.targets 中找到。
但在 Linux 和 macOS 上可以自动将 SQLite.Interop.dll 文件复制到输出目录中。在您的 csproj 文件中添加 FixSQLiteInteropFilesOnLinuxAndOSX 目标(如下所述),您将能够在没有 DllNotFoundException 的情况下在运行单声道的 Linux 和 macOS 上使用 System.Data.SQLite.Core。你的项目应该是这样的:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
</PropertyGroup>
<Target Name="FixSQLiteInteropFilesOnLinuxAndOSX" BeforeTargets="CopySQLiteInteropFiles">
<ItemGroup>
<SQLiteInteropFiles Condition="$([MSBuild]::IsOsPlatform(Linux)) OR $([MSBuild]::IsOsPlatform(OSX))" Remove="@(SQLiteInteropFiles)" />
<SQLiteInteropFiles Condition="$([MSBuild]::IsOsPlatform(Linux))" Include="$(PkgSystem_Data_SQLite_Core)/runtimes/linux-x64/native/netstandard2.0/SQLite.Interop.*" />
<SQLiteInteropFiles Condition="$([MSBuild]::IsOsPlatform(OSX))" Include="$(PkgSystem_Data_SQLite_Core)/runtimes/osx-x64/native/netstandard2.0/SQLite.Interop.*" />
</ItemGroup>
</Target>
<ItemGroup>
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.111" GeneratePathProperty="true" />
</ItemGroup>
</Project>
确保在包引用中添加GeneratePathProperty="true"。这是定义PkgSystem_Data_SQLite_Core 属性所必需的。