efcore: Exception when creating SQLite DB in when targetting osx-arm64 / .Net 6 (preview)

I’m using a SQLite provider with .Net 6.0 preview 1, and EFCore 6.0, targetting Apple Silicon (osx-arm64). When the DB creation is attempted, the following exception is thrown:

Exception: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.
Unhandled 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 shared library 'e_sqlite3' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libe_sqlite3, 1): image not found
   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.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 Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnectionState()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()

Discussed this with Rich Lander, and concluded that it’s likely to be because the native/unmanaged SQLite libraries can’t be loaded because they’re x64, not arm64.

The project where this happens is here: https://github.com/Webreaper/Damselfly - I will try and create a small standalone repro project if I get time.

EF Core version: 6.0.100-preview.1.21103.13 Database provider: SQLite Target framework: 6.0.100-preview.1.21103.13 Operating system: OSX 11.3 Beta (20E5172i) Hardware: Apple MacBook Pro M1 IDE: Visual Studio for Mac 8.9 build 1593

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 36 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Verified. This will be fixed in the RC2 (October) release.

Yeah it seems the issue is in SQLitePCLRaw.lib.e_sqlite3.

For version 2.0.4 it has the required folders: image

But for the latest 2.0.5 preview it doesn’t: image

I hope to publish a new PRE-release (which could be used to test the fix for apple silicon) today or tomorrow, pending ericsink/SQLitePCL.raw#434

ETA for the 2.0.5 release is end of August.

So the link I posted above was mostly just to give the preview version number. Replacing just the SQLitePCLRaw.core package would be insufficient.

You are probably getting SQLitePCLRaw.bundle_e_sqlite3 by way of the efcore nuget packages. To get the full preview, you need to update that nuget package to the version I mentioned:

<PackageReference Include="sqlitepclraw.bundle_e_sqlite3" Version="2.0.5-pre20210119130047" />

Really?

$ unzip -l SQLitePCLRaw.lib.e_sqlite3.2.0.5-pre20210628105421.nupkg | grep osx
  3167935  2021-05-13 15:49   runtimes/osx-x64/native/libe_sqlite3.dylib

(grumble)

Sorry. I thought I had done this part…

The Apple Silicon support for SQLitePCLRaw is currently only available in the 2.0.5 pre-release versions.

Support for Apple silicon will be in SQLitePCLRaw 2.0.5. Actually the work is done and is part of the following prerelease:

https://www.nuget.org/packages/SQLitePCLRaw.core/2.0.5-pre20210119130047

(Somebody at Microsoft whose handle I don’t remember did the actually work here and submitted it as a PR.)