Efcore: Unable to load DLL 'e_sqlite3' on EF Core 2.0

Created on 21 Aug 2017  Â·  35Comments  Â·  Source: dotnet/efcore

I'm testing my code using sqlite in memory database and I get this error when trying to run a test.

System.TypeInitializationException: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: Unable to load DLL 'e_sqlite3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Further technical details

EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.Sqlite
Operating system: Visual Studio 2017

closed-external

Most helpful comment

Is everyone here using a separate class library for their DbContext? If so, the solution to the original issue is to add a reference to SQLitePCLRaw.bundle_green to the consuming app/test projects. This is just a known/by-design limitation of how NuGet handles transitive references.

All 35 comments

Same issue after deployment to Windows10IOT - Raspberry Pi 3 (OS: 10.0.16267.1001) .
Build is produced with: dotnet publish -r win-arm

Here is a sample console app to reproduce the issue:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;

namespace HelloSqLite
{
class Program
{
static void Main(string[] args)
{
using (var db = new BloggingContext())
{
db.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
var count = db.SaveChanges();
Console.WriteLine("{0} records saved to database", count);

            Console.WriteLine();
            Console.WriteLine("All blogs in database:");
            foreach (var blog in db.Blogs)
            {
                Console.WriteLine(" - {0}", blog.Url);
            }
        }
    }
}

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    public BloggingContext()
    {
        Database.EnsureCreated();
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source=blogging.db");

    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

}

@bdominguez What framework and runtime are you using?

@glegleme According to ericsink/SQLitePCL.raw#161 this should work. You could also try just using the version that ships with Windows 10:

dotnet remove package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Sqlite.Core
dotnet add package SQLitePCLRaw.bundle_winsqlite3

@bricelam I'm targeting .NET 4.6.2

AnyCPU, x86, or x64?

NuGet via packages.config or MSBuild PackageReference items?

AnyCPU and MSBuild PackageReference

Are you using the Microsoft.NET.Test.Sdk package?

Yes, I'm using MSTest

@bricelam The solution with
dotnet add package SQLitePCLRaw.bundle winsqlite3
does not work against dot.net core console app, because that package is compatible with UAP 10 only.
I have tried that workaround, but to no avail. It compiles and work on development machine just fine, but on the target machine.
I have also tried out several other bundles.

@bricelam info that can be of your interest on *.csproj:

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

  <PropertyGroup>
    <TargetFramework>net462</TargetFramework>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.1.18" />
    <PackageReference Include="MSTest.TestFramework" Version="1.1.18" />
  </ItemGroup>

</Project>

Solution (at least for raspberry Pi) is following:

  • find winsqlite3.dll (It is located in WINDOWS/System32) on Raspberry Pi
  • copy this dll into publish destination for .net core project
  • rename it to: e_sqlite3.dll
    Start dotnet console application.
    This works both for console as well as for Asp.Net MVC core application.

@glegleme Can you follow up with an issue on ericsink/SQLitePCL.raw? AFAIK, it's supposed to just workâ„¢.

@bdominguez Could you share your solution? I'm having a hard time reproducing the issue...

@bricelam After follow up with https://github.com/ericsink/SQLitePCL.raw/issues/175 the solution is to use

dotnet remove package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Sqlite.Core
dotnet add package SQLitePCLRaw.bundle_e_sqlite3
dotnet publish -r win10-arm

dotnet publish -r win10-arm

The build instructions on https://github.com/dotnet/core/blob/master/samples/RaspberryPiInstructions.md are missleading.

@bricelam here you have:

App.zip

When you run the test you should see:

Message: Test method Test.UnitTest1.Test threw exception: 
System.TypeInitializationException: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: Unable to load DLL 'e_sqlite3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at SQLitePCL.SQLite3Provider_e_sqlite3.NativeMethods.sqlite3_libversion_number()
   at SQLitePCL.SQLite3Provider_e_sqlite3.SQLitePCL.ISQLite3Provider.sqlite3_libversion_number()
   at SQLitePCL.raw.SetProvider(ISQLite3Provider imp)
   at SQLitePCL.Batteries_V2.Init()
--- End of inner exception stack trace ---
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Data.Sqlite.Utilities.BundleInitializer.Initialize()
   at Microsoft.Data.Sqlite.SqliteConnection..cctor()
--- End of inner exception stack trace ---
    at Microsoft.Data.Sqlite.SqliteConnection..ctor(String connectionString)
   at Database.Db.OnConfiguring(DbContextOptionsBuilder optionsBuilder)
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_ExecutionStrategyFactory()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.CreateExecutionStrategy()
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.OpenConnection(DatabaseFacade databaseFacade)
   at Database.Db..ctor()
   at Test.UnitTest1.Test() in E:\Downloads\App\Test\UnitTest1.cs:line 12

@bdominguez This looks like an issue with transitive packages in project references in NuGet. Adding the following to Test.csproj makes it work.

<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" />

Can you file a new issue on NuGet/Home?

@bdominguez Did you file an issue yet? If so, could you reference it from here?

@ajcvickers done.

I wonder if adding PrivateAssets="None" to the original reference would also work. It may just not be propagating a required MSBuild script...

I still get this error - solved it via copy / rename from Windows32 my project.json

 <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>

Iam also still getting this error

Still having this error in Windows 10, in spite adding
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" />
as suggested.
Weird thing, I had no such error in macOS (probably due to sqlite being part of the system)

The error still comes up.

Is everyone here using a separate class library for their DbContext? If so, the solution to the original issue is to add a reference to SQLitePCLRaw.bundle_green to the consuming app/test projects. This is just a known/by-design limitation of how NuGet handles transitive references.

I have built a small project in macOS.
Following your advice I've just run
dotnet add package SQLitePCLRaw.bundle_green
which completed OK and
dotnet publish -r win-x86 -c release
to also run in a Windows machine, which is a requirement.

The same error appears (i.e. that the dll e_sqlite.dll is missing, as per screenshot link)
https://imgur.com/a/GmDfa

In macOS the project continues to run OK, without complains.

Ah, you need to use win7-x86 at least. SQLitePCL.raw doesn't include a dll for win-x86.

You are right!
I thought by looking at this that the Release Configuration _could_ just be win-x86 or win-x64

Yeah, I'm not sure why NuGet doesn't just treat win-x* and win7-x* as aliases during publish...

(and win-arm and win8-arm)

@bricelam
"Is everyone here using a separate class library for their DbContext? If so, the solution to the original issue is to add a reference to SQLitePCLRaw.bundle_green to the consuming app/test projects. This is just a known/by-design limitation of how NuGet handles transitive references."

Yes, I am trying to do just that... is there any way of bypassing this limitation? I want to deliver the customer just one DLL containing the database layer... don't want him to have to download extra references.

@bricelam, @ajcvickers I am still facing this issue even after trying all the answers in this thread. Does this issue still persist in later builds ?
I am also using Sqlite db for unit testing with specflow.

Technical Details:
Microsoft.EntityFrameworkCore 2.2.4
Microsoft.Data.Sqlite.Core 2.2.4

This is still an issue.

I get this error when trying to publish a portable PowerScript C# module (dotnet publish) but not when making it Win10-specific (dotnet publish -r win10-x64). My module targets .NET Standard 2.1.

I have tried adding references to Microsoft.Data.Sqlite and SQLitePCLRaw.bundle_green but they don't help.

Any ideas anyone as I'm trying to create a cross-platform PowerShell module....

cc @ericsink (in case you've seen similar reports)

Nope, I haven't seen this one before.

I don't know anything about PowerShell modules or PowerScript, but I assume that something about the environment being presented is different in a way that is causing problems for finding the native DLL.

The fact that the error does not happen with publish -r win10-x64 is interesting. That suggests that we need to figure out a way to get other DLLs forced to be included.

Adding SQLitePCLRaw.bundle_green to the consuming app/test projects does not work for me. Error still appears.

Was this page helpful?
0 / 5 - 0 ratings