System.Data.SQLite from NuGet, interop dll not cop

2020-03-09 06:59发布

I installed System.Data.SQLite Core (x86/x64) from NuGet. It built without warnings but threw System.DllNotFoundException regarding SQLite.Interop.dll. I rigged my projects to copy the SQLite.Interop.dll from under the NuGet package's directory to the output directory, and now it runs without the exception.

Why didn't the NuGet package configure my projects to put the appropriate interop dll in the output directory? It seems like it should be able to do that.

I'm new to interop and I inherited this codebase, which previously referenced System.Data.SQLite.dll directly by path. I switched to NuGet to get rid of warnings about a mismatch between the processor architecture of the project vs System.Data.SQLite. I'm trying to build all projects as AnyCPU.

10条回答
仙女界的扛把子
2楼-- · 2020-03-09 07:38

In my case, the problem was the fact that I was using SQLite inside a class library project that was then used by another WPF (gui type) project.

Solved the SQL.Interop.dll not getting copied to output directory, by using the following Post-Build command, inside Project Properties -> Build Events:

xcopy "$(SolutionDir)packages\System.Data.SQLite.Core.1.0.101.0\build\net451\x86\SQLite.Interop.dll" "$(OutputDir)" /y /f

/y overwrites
/f displays actual filenames being copied
查看更多
看我几分像从前
3楼-- · 2020-03-09 07:41

In my case the SQL.Interop.dll was not copied by Nuget in any way, manually put the right version of the dll in the x86 and x64 folder solved the issue.

If you've installed Sqlite from Nuget, you can find the SQL.Interop.dll in this folder (for .NET 4.0)

PROJECT_FOLDER\packages\System.Data.SQLite.Core.1.0.*.*\build\net40

查看更多
唯我独甜
4楼-- · 2020-03-09 07:43

In my case the myProject.csproj file did not have the System.Data.SQLite.Core.targets defined. I added the following line and both x64 and x86 versions of SQLite.Interop.dll are now copied for all build targets.

<Import Project="..\packages\System.Data.SQLite.Core.1.0.98.1\build\net45\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.98.1\build\net45\System.Data.SQLite.Core.targets')" />

I'm not sure what will happen when the NuGet package for System.Data.SQLite.Core gets updated and if the package path will need to be manually altered.

查看更多
乱世女痞
5楼-- · 2020-03-09 07:47

With the System.Data.SQLite.Core NuGet package version 1.0.104, I had the same problem as @Eternal21 and @Patrick. That is, project A references SQLite and project B references A where SQlite.Interop.dll is not copied into the output directory of B.

I found a solution that solves the trouble in project A rather than B which is a more robust solution since it fixes the problem once for all future projects refering to A. The .targets file of the NuGet package contains the following section:

<ItemGroup Condition="'$(ContentSQLiteInteropFiles)' != '' And
                      '$(ContentSQLiteInteropFiles)' != 'false' And
                      '@(SQLiteInteropFiles)' != ''">
  <Content Include="@(SQLiteInteropFiles)">
    <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </Content>
</ItemGroup>

This section adds SQLite.Interop.dll as a reference that has to be copied to project A's output and also to the output of refering projects (like B). But the MSBuild property ContentSQLiteInteropFiles is undefined by default (I don't know why) disabling the reference by the first condition. To enable it, I added the following line to a PropertyGroup element of projects A's .csproj file:

<ContentSQLiteInteropFiles>true</ContentSQLiteInteropFiles>

Please note that this line must prececde the Import element for the .targets file of the NuGet package.

查看更多
Animai°情兽
6楼-- · 2020-03-09 07:51

in my case using NuGet for installing SQLite and still I need to add manually SQliteinterop.dll as a Resource. Then I build muy proyect and when I publish it works fine. (Working with x86 configuration)

查看更多
女痞
7楼-- · 2020-03-09 07:53

I have a DLL project that uses the SQLite package from nuget but a test project for it would always raise the DLL not found exception.

The simplest solution I found was to add the SQLite nuget package to the test project too.

查看更多
登录 后发表回答