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.
C#
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
}
@dyoalie I have not been able to reproduce this. Please post a small, runnable project that reproduces the behavior you are seeing.
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.
I have the same bug described above. With same setup.
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?
@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?
Possibly related to https://github.com/ericsink/SQLitePCL.raw/issues/252
The workaround should be to add a reference to SQLitePCLRaw in the test project
@bricelam Which package specifically? I added this, but no luck:
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.0.2" />
That should've been enough. I'll have to dig into it.
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.
Duplicate #19893 reports this also for xUnit, so it's likely not test framework-specific.
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()
I have the same problem targeting net462 and using xunit 2.41.
Problem also occurs when using: Microsoft.Data.Sqlite 3.1.0
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: https://github.com/dotnet/efcore/issues/19893
--- ngm
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
I have the same problem.
Step to reproduce :
<TargetFramework>net48</TargetFramework>
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; }
}
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>
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.
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
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?
I have not found have a solution, but my tests run fine when using NCrunch or ReSharper/dotCover. To get my tests to run in NCrunch, I needed to set "Copy Referenced Assemblies to Workspace" to On.
Still, I would like this to work in the Visual Studio test runner.
I'm getting this same error with .NET Framework 4.7.2. I am referencing a .Net Standard 2.0 class library on a WPF project targetting .NetFramework 4.7.2.
When Visual Studio is running the unit tests (xUnit), the net48 DLLs are copied to a AppData\Local\Temp
folder and executed from there. The native DLLs are not copied. That leads to this error.
I've created a hack to workaround the issue. It copies the runtimes
folder from the Assembly.Location to Assembly.CodeBase folders. In my case, I only need this when running unit tests, so I'm fine with the ugliness of the hack. It
https://gist.github.com/FuncLun/fff2c63c7d37f4ff3d20f70e00bb241f
@FuncLun you saved me from serious headache! Many thanks!
@FuncLun Thank you!
I was getting this in a .net 4.8 framework asp.net mvc site due to the shadow cache (It was copying it to C:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET Filesvsc0b0bd77359dbc76assemblydl31a57f6b2002f8908_9690d501SQLitePCLRaw.core.dll)
Unfortunately it was manifesting as a null exception which threw me off the trail for a while.
@FuncLun Thank you. It saved my day.
cc @ericsink
Another way to workaround this issue that doesn't involve adding code in your test project is to disable xUnit test runner shadow copy. Here is how:
xunit.runner.json
file with the following content{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"shadowCopy": false
}
<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
Here's how I did it on @MatthewKing's sample code: https://github.com/0xced/Issue19396/commit/73d1504b2698b3db23309e40364b4e943fab315e
And once you know the solution, it becomes easier to find where it was already discussed. ¯_(ツ)_/¯
Disabling shadow copying in xunit helped me.
Also another option for xunit.runner.console is to use -noshadow
For me, it worked by adding the e_sqlite3 bundle to the executable (.net framework) project
I've hit this as well with a .NET Core 3.1 PowerShell cmdlet running under PowerShell 7.0.3. The build seems to place assemblies as expected
so it appears something's off with the directory probing used. As with the other folks, my workaround's to copy the correct e_sqlite3.dll to
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' or one of its dependencies: The specified module could not be found. (0x8007007E)
at System.Runtime.InteropServices.NativeLibrary.LoadByName(String libraryName, QCallAssembly callingAssembly, Boolean hasDllImportSearchPathFlag, UInt32 dllImportSearchPathFlag, Boolean throwOnError)
at System.Runtime.InteropServices.NativeLibrary.LoadLibraryByName(String libraryName, Assembly assembly, Nullable`1 searchPath, Boolean throwOnError)
at System.Runtime.InteropServices.NativeLibrary.Load(String libraryName, Assembly assembly, Nullable`1 searchPath)
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)
at SQLitePCL.Batteries_V2.Init()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
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 Cmdlets.MyCmdlet.ProcessRecord()
at System.Management.Automation.Cmdlet.DoProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
Was having this issue when running a WPF app. I installed SQlitePCLRaw.bundle_e_sqlite3 v 2.0.4 and the error went away.
Most helpful comment
When Visual Studio is running the unit tests (xUnit), the net48 DLLs are copied to a
AppData\Local\Temp
folder and executed from there. The native DLLs are not copied. That leads to this error.I've created a hack to workaround the issue. It copies the
runtimes
folder from the Assembly.Location to Assembly.CodeBase folders. In my case, I only need this when running unit tests, so I'm fine with the ugliness of the hack. Ithttps://gist.github.com/FuncLun/fff2c63c7d37f4ff3d20f70e00bb241f