efcore icon indicating copy to clipboard operation
efcore copied to clipboard

PlatformTarget AnyCPU causes SQLite invocation to fail with cryptic error

Open ketrex2 opened this issue 9 years ago • 4 comments

I have a .NET core console application which references a netstandard1.4 class library containing a dbContext. My console application creates a new ServiceCollection and adds the necessary references as detailed here:

var services = new ServiceCollection();
            services.AddOptions();
            services.AddSingleton(_configuration);
            services.AddLogging();
            services.AddEntityFramework();
            var connectionString =
                    new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder { DataSource = "etoDataMgr.db" }.ToString();
                services.AddDbContext<LocalDataContext>(o => o.UseSqlite(connectionString));

As long as I specify x64 or x86 as the platform target, my application runs fine. If I set the target to AnyCPU, an exception is thrown during the call to 'Database.EnsureCreated()' of my DbContext.

If the SQLite provider requires an explicit target for x86 and x64, then the error message for 'AnyCPU' is pretty cryptic, and not terribly helpful in tracking down the problem.

The exception is:

Exception message:
Unable to load DLL 'sqlite3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Stack Trace:
   at Microsoft.Data.Sqlite.Interop.NativeMethods.Sqlite3_sqlite3.sqlite3_open_v2(IntPtr filename, Sqlite3Handle& ppDb, Int32 flags, IntPtr vfs)
   at Microsoft.Data.Sqlite.Interop.NativeMethods.sqlite3_open_v2(String filename, Sqlite3Handle& ppDb, Int32 flags, String vfs)
   at Microsoft.Data.Sqlite.SqliteConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqliteRelationalConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqliteDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()
   at Omni.EtoDataMgr.Data.LocalDataContext..ctor(DbContextOptions dbContextOptions)

Steps to reproduce

Specify 'AnyCPU' as the platform target.

Further technical details

EF Core version: 1.1.2 Database Provider: Microsoft.EntityFrameworkCore.Sqlite 1.1.2 Operating system: Windows 10 Pro x64 IDE: Visual Studio 2017

ketrex2 avatar Jul 29 '17 18:07 ketrex2

I've seen this too. At some point, I think the SQLite NuGet Package stopped working with AnyCPU projects. You could try downgrading that package to see if it works with an older one.

bricelam avatar Jul 31 '17 15:07 bricelam

It's supposed to copy both versions of the dll to the output directory: x86\sqlite3.dll and x64\sqlite3.dll.

bricelam avatar Jul 31 '17 16:07 bricelam

This issue was moved to aspnet/Microsoft.Data.Sqlite#393

divega avatar Aug 11 '17 08:08 divega

The issue is in the third-party SQLite package. The logic for building it is in natemcmaster/libsqlite3-package.

bricelam avatar Aug 11 '17 17:08 bricelam