dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.75k stars 3.18k forks source link

Unable to load DLL 'e_sqlite3' on EF Core 2.0 #9505

Closed bdominguez closed 2 years ago

bdominguez commented 7 years ago

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

glegleme commented 7 years ago

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; }
}

}

bricelam commented 7 years ago

@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
bdovaz commented 7 years ago

@bricelam I'm targeting .NET 4.6.2

bricelam commented 7 years ago

AnyCPU, x86, or x64?

bricelam commented 7 years ago

NuGet via packages.config or MSBuild PackageReference items?

bdominguez commented 7 years ago

AnyCPU and MSBuild PackageReference

bricelam commented 7 years ago

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

bdominguez commented 7 years ago

Yes, I'm using MSTest

glegleme commented 7 years ago

@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.

bdominguez commented 7 years ago

@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>
glegleme commented 7 years ago

Solution (at least for raspberry Pi) is following:

bricelam commented 7 years ago

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

bricelam commented 7 years ago

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

glegleme commented 7 years ago

@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.

bdominguez commented 7 years ago

@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
bricelam commented 7 years ago

@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?

ajcvickers commented 7 years ago

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

bdominguez commented 7 years ago

@ajcvickers done.

bricelam commented 7 years ago

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

ManniAT commented 7 years ago

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>
ms-141630 commented 7 years ago

Iam also still getting this error

gtaranti commented 7 years ago

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)

kudilovich commented 7 years ago

The error still comes up.

bricelam commented 7 years ago

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.

gtaranti commented 7 years ago

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.

bricelam commented 7 years ago

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

gtaranti commented 7 years ago

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

bricelam commented 7 years ago

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)

radutomy commented 6 years ago

@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.

giri-nitesh commented 5 years ago

@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

todduk commented 4 years ago

This is still an issue.

markm77 commented 4 years ago

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....

bricelam commented 4 years ago

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

ericsink commented 4 years ago

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.

alelom commented 4 years ago

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