26

My ASP.NET MVC (.Net Framework 4.7.2) web app using Microsoft.Data.Sqlite 5.0.2 crashes (both in debug IIS Express, and app.publish on local IIS in Windows 10) at line:

SqliteConnection dbConn = new SqliteConnection("Data Source=test.db");

The exception thrown :

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.
  Source=Microsoft.Data.Sqlite
  StackTrace:
   at Microsoft.Data.Sqlite.SqliteConnection..ctor(String connectionString)
   at sqliteTest.Controllers.ValuesController.Get() in C:\Users\FaqeerHussain\source\repos\sqliteTest\sqliteTest\Controllers\ValuesController.cs:line 16
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
TargetInvocationException: Exception has been thrown by the target of an invocation.

Inner Exception 2:
Exception: Library e_sqlite3 not found

e_sqlite3.dll is already in bin\runtimes\win-x64\native and bin\runtimes\win-x86\native. Tried copying x86/x64 e_sqlite3.dll to root bin folder but error remains. What should I do to get Microsoft.Data.Sqlite working?

13 Answers 13

13

This is because the package of Microsoft.Data.Sqlite is not compatible with the version of .Net Framework. You can try to change the version of NuGet of Microsoft.Data.Sqlite to 2.2.0 to run normally.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

13

I found that the e_sqlite3.dll is located at runtimes\win-x86\native\e_sqlite3.dll. So I created those folders and added the e_sqlite3.dll in the native folder. After that it worked!

Edit: Create folder at: C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\runtimes\win-x64\native (match your vs version).

Copy from: C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions and search the e_sqlite3.dll because the folder name can be different.

1 Comment

This also solved the issue for me. In case anyone is wondering about the location of the 'e_sqlite3.dll' files, they were in my case in the 'bin/Debug' directory, within the subfolders 'x64' and 'x86'.
7

I think the problem here is that Microsoft.Data.Sqlite depends on SQLitePCLRaw.bundle_e_sqlite3, and SQLitePCLRaw.bundle_e_sqlite3 depends on SQLitePCLRaw.lib.e_sqlite3, where SQLitePCLRaw.lib.e_sqlite3 does not have actual DLL as it provides native binary directly. So when I hit this issue I found that SQLitePCLRaw.lib.e_sqlite3 NuGet and it's build targets are not present in my csproj upon installation. So when project is being build by VS or MSBuild both of them are not aware that e_sqlite3 native binary has to be copied into bin folder during the build.

I did solve the issue by adding targets manually into csproj:

<Project>

  ...

  <Import Project="..\..\packages\SQLitePCLRaw.lib.e_sqlite3.2.1.5\buildTransitive\net461\SQLitePCLRaw.lib.e_sqlite3.targets" Condition="Exists('..\..\packages\SQLitePCLRaw.lib.e_sqlite3.2.1.5\buildTransitive\net461\SQLitePCLRaw.lib.e_sqlite3.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\..\packages\SQLitePCLRaw.lib.e_sqlite3.2.1.5\buildTransitive\net461\SQLitePCLRaw.lib.e_sqlite3.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\SQLitePCLRaw.lib.e_sqlite3.2.1.5\buildTransitive\net461\SQLitePCLRaw.lib.e_sqlite3.targets'))" />
  </Target>

  ...

</Project>

1 Comment

This is the only solution that worked for me. The known issue (github.com/dotnet/efcore/issues/19396) states this is resolved with the 8.0.0+ version of Microsoft.Data.Sqlite but I can still replicate the problem in VS2017.
6

Dump Microsoft.Data.Sqlite in favor of System.Data.Sqlite. It supports version 3.38 databases with upserts an other perks and has no such issues. I did and never looked back.

The difference will be in the casing of the names:

using System.Data.SQLite;

Instead of:

SqliteConnectionStringBuilder
SqliteConnection
SqliteCommand
SqliteTransaction
SqliteType.Text

you will use, respectively:

SQLiteConnectionStringBuilder
SQLiteConnection
SQLiteCommand
SQLiteTransaction
DbType.String etc

1 Comment

sqlite is only about problems and missing file, now this
6

I had a similar issue and it was caused by mixing of the packages.config and PackageReference. (I'm working on some old legacy software right now) I Right-Clicked the packages.config and selected Migrate packages.config to PackageReference and it fixed the issue where it wasn't able to find the e_sqlite3.dll when calling SQLitePCL.Batteries.Init(); Additionally, I no longer needed to call SQLitePCL.Batteries.Init(); so I removed that.

1 Comment

This solved for me.
3

It's a known issue that hasn't been fixed yet.

I was referencing a .NET Standard 2.0 library that was using SQLite, from a .NET Framework 4.8 project. I changed library to also target .NET 4.8 as a workaround since it wasn't used by any other implementation of .NET. I had to change <TargetFramework>netstandard2.0</TargetFramework> to <TargetFramework>net48</TargetFramework> in my SQLite project.

1 Comment

This solved the problem. A little sad, but it'll do. "e_sqlite3.dll" was added to the output folder after I set it to framework 4.8. And a "runtimes" folder was created in the project too.
0

For a Maui multiplatform app make sure your have the following in your project file (update to actual version):

    <ItemGroup>    
        <PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.2" />
    </ItemGroup>

    <ItemGroup>
      <None Remove="SQLitePCLRaw.bundle_e_sqlite3" />
    </ItemGroup>

Comments

0

My ASP.NET (.Net Framework 4.7) web app in which I used Microsoft.Data.Sqlite 5.0.10 gives :

Exception: Library e_sqlite3 not found

I removed dependency library of Microsoft.Data.Sqlite and Microsoft.Data.Sqlite.Core, SQLitePCLRaw.bundle_e_sqlite3, SQLitePCLRaw.core, SQLitePCLRaw.lib.e_sqlite3, SQLitePCLRaw.provider.dynamic_cdecl, etc., from package folder. I added Microsoft.Data.Sqlite version 6.0.0 along with its dependencies. My app is working now. I only used this library to read SpatiaLite database.

Comments

0

Had this error in NUnit 2 GUI for .NET Framework 4.7.2 test library using Microsoft.Data.Sqlite 5.0.0. Found xUnit solution here, which is to disable shadow copy.

Comments

0

I had to go back to version of Microsoft.Data.Sqlite that used to work for my older projects (4.7.2 and pre) to 1.0.0. And I did check: the platform subfolders and DLL's were there.

Comments

0

This problem is still there in ALL the latest releases! I cannot believe this is still going on maybe 5 years after the issue was first reported.

I fiddled with this for an age before I got it working. The simple solution was eventually found.

Build the project for AnyCPU

Copy e_sqlite3.dll from release\x86 folder to the same folder as the main executable

App then runs, no error.

Comments

0

I found the issue, may not be the same issue for everyone. Look at your project file, and search for e_sqlite3. You should see a line like this:

<Import Project="..\..\packages\SQLitePCLRaw.lib.e_sqlite3.2.1.8\buildTransitive\net461\SQLitePCLRaw.lib.e_sqlite3.targets" Condition="Exists('..\..\packages\SQLitePCLRaw.lib.e_sqlite3.2.1.8\buildTransitive\net461\SQLitePCLRaw.lib.e_sqlite3.targets')" />

This tells Visual Studio where to get the targets file, which in turn determines where to put the e_sqlite file.

In my case, this was the wrong location, it was going one more directory up than it needed to. I changed ....\ to ..\ and everything was found and the file was put into the right location.

The correct project file line should be:

<Import Project="..\packages\SQLitePCLRaw.lib.e_sqlite3.2.1.8\buildTransitive\net461\SQLitePCLRaw.lib.e_sqlite3.targets" Condition="Exists('..\packages\SQLitePCLRaw.lib.e_sqlite3.2.1.8\buildTransitive\net461\SQLitePCLRaw.lib.e_sqlite3.targets')" />

Comments

-1

If you downloaded Microsoft.Data.Sqlite using NuGet: Use the directory \Users"UserName".nuget\packages\sqlitepclraw.lib.e_sqlite3"Version"\runtimes\win-x64\native\e_sqlite3.dll Where "Version" is not e_sqlite3.dll version, but Microsoft.Data.Sqlite For e_sqlite3.dll version, execute cmd.CommandText = "select sqlite_version();"

1 Comment

This is poorly formatted, and very difficult to understand what you're trying to say; you should consult the markdown formatting guide: stackoverflow.com/editing-help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.