Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[.Net Framework 4.8]Library e_sqlite3 not found #19396

Closed
dyoalie opened this issue Dec 24, 2019 · 62 comments
Closed

[.Net Framework 4.8]Library e_sqlite3 not found #19396

dyoalie opened this issue Dec 24, 2019 · 62 comments
Assignees
Labels
area-adonet-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported propose-close punted-for-5.0 punted-for-6.0 type-bug
Milestone

Comments

@dyoalie
Copy link

dyoalie commented Dec 24, 2019

A .Net Framework 4.8 program referencing a .Net Standard 2.0 using EFCore.Sqlite 3.1 can't simply run (on Any CPU/X64/X86) with an exception shown:

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.Exception: Library e_sqlite3 not found

   at SQLitePCL.NativeLibrary.Load(String libraryName, Assembly assy, Int32 flags)
   at SQLitePCL.Batteries_V2.MakeDynamic(String name, Int32 flags)
   at SQLitePCL.Batteries_V2.DoDynamic_cdecl(String name, Int32 flags)
   --- 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 Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Test.Program.Main(String[] args) in E:\Edit\CS\WPF\Memo\Test\Program.cs:line 22

But the output folder does contain e_sqlite3.dll, which seems to be a bug.

To Reproduce

				var connection = "DataSource=default.db";
				var builder = new DbContextOptionsBuilder<Db>();
				builder.UseSqlite(connection);
				builder.EnableSensitiveDataLogging();
				var options = builder.Options;
				using (var db = new Db(options))
				{
					db.Database.Migrate(); //this line throws that exception
				}
@ajcvickers
Copy link
Member

@dyoalie I have not been able to reproduce this. Please post a small, runnable project that reproduces the behavior you are seeing.

@ajcvickers
Copy link
Member

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

@Hassan1984
Copy link

I have the same bug described above. With same setup.

@MatthewKing
Copy link

G'day @ajcvickers I'm not the original submitter of this issue, but I think I'm running in to the same thing - except on net472.

I've put together a very very simple project that reproduces the issue. Doesn't even need EFCore, just Microsoft.Data.Sqlite is sufficient to reproduce the issue. https://github.com/MatthewKing/Issue19396

Unless I'm doing something wrong?

@ajcvickers ajcvickers reopened this Jan 27, 2020
@ajcvickers
Copy link
Member

@MatthewKing Thanks!

@bricelam This repros for me in Visual Studio when running tests. However, it works when using Rider to run the tests. Maybe the VS test runner isn't resolving the native path correctly?

@bricelam
Copy link
Contributor

Possibly related to ericsink/SQLitePCL.raw#252

@bricelam
Copy link
Contributor

The workaround should be to add a reference to SQLitePCLRaw in the test project

@ajcvickers
Copy link
Member

@bricelam Which package specifically? I added this, but no luck:

<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.0.2" />

@bricelam
Copy link
Contributor

That should've been enough. I'll have to dig into it.

@masconejos
Copy link

I'm experiencing this issue too. It may be helpful to note that I can use the PM Console to create/run migrations and everything works as expected. However when I attempt to run my code, the second I try to query for data it hangs for a second then times out and raises the exception.

@ajcvickers
Copy link
Member

Duplicate #19893 reports this also for xUnit, so it's likely not test framework-specific.

@johnholliday
Copy link

Also getting this for xUnit. Stack trace follows:

This exception was originally thrown at this call stack:
	SQLitePCL.NativeLibrary.Load(string, System.Reflection.Assembly, int)
	SQLitePCL.Batteries_V2.MakeDynamic(string, int)
	SQLitePCL.Batteries_V2.DoDynamic_cdecl(string, int)
	SQLitePCL.Batteries_V2.Init()

@paulyii
Copy link

paulyii commented Feb 16, 2020

I have the same problem targeting net462 and using xunit 2.41.
Problem also occurs when using: Microsoft.Data.Sqlite 3.1.0

@ngm011
Copy link

ngm011 commented Feb 18, 2020

I have the same problem targeting net462 and using xunit 2.41.
Problem also occurs when using: Microsoft.Data.Sqlite 3.1.0

I've got the same issue running on .NET Framework v4.8 and xUnit Test Runner for Visual Studio v2.4.1: #19893

--- ngm

@earldean
Copy link

I was able to duplicate this in a slightly different manner. I encountered the same exception, although it was a single .Net 4.7.2 Class library which references EFCoreSqlite. I set up a single test case, which when run throws the exception. The constructor in the test class just tries to create a new SqliteConnection

Here is the repo with the sln I made to duplicate this

@vernou
Copy link

vernou commented Mar 22, 2020

I have the same problem.
Step to reproduce :

  1. Create a xUnit .Net Core Project from Visual Studio 16.5.0
  2. Edit csproj to .NET Framework 4.8 :
<TargetFramework>net48</TargetFramework>
  1. Add NuGet Package Microsoft.EntityFrameworkCore.Sqlite 3.1.2
  2. Add the test :
public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        var options = new DbContextOptionsBuilder().UseSqlite("DataSource=:memory:").Options;
        using (var context = new MyContext(options))
        {
            context.MyEntities.ToList();
        }
    }
}

public class MyEntity
{
    public int Id { get; set; }
    public string Label { get; set; }
}

public class MyContext : DbContext
{
    public MyContext(DbContextOptions options) : base(options)
    { }

    public DbSet<MyEntity> MyEntities { get; set; }
}
  1. Run the test in Visual Studio Text Explorer
  2. Get the error :
Message: 
    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.Exception : Library e_sqlite3 not found
Stack Trace: 
    SqliteConnection.ctor(String connectionString)
    SqliteRelationalConnection.CreateDbConnection()
    RelationalConnection.get_DbConnection()
    RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
    RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
    Enumerator.InitializeReader(DbContext _, Boolean result)
    NoopExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
    Enumerator.MoveNext()
    List`1.ctor(IEnumerable`1 collection)
    Enumerable.ToList[TSource](IEnumerable`1 source)
    UnitTest1.Test1() line 16

But the test work with .NET Core runtime :

<TargetFramework>netcoreapp3.1</TargetFramework>

@lucahost
Copy link

lucahost commented Mar 30, 2020

I'm experiencing the same bug.

I've created a new cli app using dotnet new

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.1;net48</TargetFrameworks>

Using VisualStudio and running the CLI using netcoreapp3.1 works fine.
image

However choosing net48 fails with the above mentioned error message

The problem happens seems to happen in my efcore-sqlite-class-lib which targets netstandard2.0

@jeremy-morren
Copy link

I'm getting this same error with .NET Framework 4.7.2. It is definitely a problem with the Visual Studio Test runner as identical code works fine when run as a regular application. I have added literally every sqlite package to the Test Project to no avail. Does anyone have any suggestions?

@GarryLowther
Copy link

I had similar trouble on a .Net Framework 4.7.2 console app with VS2022 on Windows 11 64-bit trying to get this code to run:
https://github.com/dotnet/docs/blob/main/samples/snippets/standard/data/sqlite/InMemorySample/Program.cs

I solved it by adding this line of code:
SQLitePCL.Batteries.Init();
before the instantiating of the connection, and then by adding these packages via NuGet:
<packages> <package id="Microsoft.Bcl" version="1.1.8" targetFramework="net472" /> <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net472" /> <package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net472" /> <package id="Microsoft.Data.Sqlite" version="6.0.2" targetFramework="net472" /> <package id="Microsoft.Data.Sqlite.Core" version="6.0.2" targetFramework="net472" /> <package id="MultiTarget.Microsoft.Data.Sqlite.Core" version="2.1.1" targetFramework="net472" /> <package id="SQLitePCLRaw.bundle_e_sqlite3" version="2.0.7" targetFramework="net472" /> <package id="SQLitePCLRaw.core" version="2.0.7" targetFramework="net472" /> <package id="SQLitePCLRaw.lib.e_sqlite3" version="2.0.7" targetFramework="net472" /> <package id="SQLitePCLRaw.provider.dynamic_cdecl" version="2.0.7" targetFramework="net472" /> <package id="SQLitePCLRaw.provider.e_sqlite3" version="2.0.7" targetFramework="net472" /> <package id="System.Buffers" version="4.4.0" targetFramework="net472" /> <package id="System.Memory" version="4.5.3" targetFramework="net472" /> <package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net472" /> <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" /> </packages>

@pvoosten
Copy link

@ericsink suggested a solution here: ericsink/SQLitePCL.raw#485

@viceice
Copy link

viceice commented Aug 1, 2022

For me it works when explicit reference a newer version SQLitePCLRaw.bundle_e_sqlite3

<PackageVersion Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.0" />

i think v2.0.7 fixes this.

@bricelam bricelam added propose-close verify-fixed This issue is likely fixed in new query pipeline. labels Apr 4, 2023
@bricelam bricelam removed their assignment Jul 8, 2023
@ajcvickers
Copy link
Member

Verified that this is fixed with M.D.Sqlite 8.0.0 targeting net472.

@ajcvickers ajcvickers modified the milestones: Backlog, 8.0.0 Dec 8, 2023
@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed verify-fixed This issue is likely fixed in new query pipeline. labels Dec 8, 2023
@vernou
Copy link

vernou commented Dec 12, 2023

Verified that this is fixed with M.D.Sqlite 8.0.0 targeting net472.

Nice, can you explain how to apply the fix?

@ajcvickers
Copy link
Member

Install that latest package.

@iSantPy
Copy link

iSantPy commented Jun 25, 2024

I am facing this issue but not when runing my project in vs (c#, winforms and vs2022) but when I build a .exe using visual studio project installer 2022. After I install the app and run it the error appears. It is weird since there is no error message when building the .exe.

I am using MS.EF.Core.Sqlite (8.0.6) and MS.EFCore (8.0.6) to manage the sqlite db.

I create the dbcontext as follow:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; optionsBuilder.UseSqlite(connectionString); }

I am using .NET8.0

@vernou
Copy link

vernou commented Jun 26, 2024

@iSantPy
Maybe the installer don't include the folder "bin/runtimes/".

What is "visual studio project installer 2022"?
When the error occurs? During build? After installation and run on other machine?

@iSantPy
Copy link

iSantPy commented Jun 26, 2024

@iSantPy Maybe the installer don't include the folder "bin/runtimes/".

What is "visual studio project installer 2022"? When the error occurs? During build? After installation and run on other machine?

microsoft visual studio installer project: https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2022InstallerProjects

it is used to build a .exe for windows.

runtimes folder

the runtimes folder is there and contains the directory runtimes\win-x86\native\e_sqlite3.dll with the file that is "missing".

Reading with detail the message when building the .exe I notice this:

message

maybe there could be the problem?

I install the app and then I run it and the problem appers. But if I run it from vs it runs ok. I have not proved in other machine yet

@vernou
Copy link

vernou commented Jun 26, 2024

@iSantPy
Maybe the installer don't include the folder "bin/runtimes/".

When the application is installed, the files are copied in a installation folder, generally in C:\Program Files or C:\Program Files (x86). Can you try to locate this folder and check if the folder runtimes is present?

@iSantPy
Copy link

iSantPy commented Jun 26, 2024

@iSantPy Maybe the installer don't include the folder "bin/runtimes/".

When the application is installed, the files are copied in a installation folder, generally in C:\Program Files or C:\Program Files (x86). Can you try to locate this folder and check if the folder runtimes is present?

Sure!

here are the screenshots:

file

runtimes folder

You talk about a bin folder that in my case it is not there after installing. Could be that a problem?

@vernou
Copy link

vernou commented Jun 26, 2024

If you execute "ControlAguaPotable.exe" by double clicking on, does the error occur?

@iSantPy
Copy link

iSantPy commented Jun 26, 2024

If you execute "ControlAguaPotable.exe" by double clicking on, does the error occur?

No, it runs ok

app runs ok

but when I initialize the dbContext to read or write the db the errors appears:

error

and only happens when I run the .exe from the installation folder. If I run the .exe from the output folder created by vs (debug or release) it runs ok and it reads and writes the db correctly.

@vernou
Copy link

vernou commented Jul 1, 2024

@iSantPy , Can you do a repos with a minimal reproducible example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-adonet-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported propose-close punted-for-5.0 punted-for-6.0 type-bug
Projects
None yet
Development

No branches or pull requests