PlatformTarget AnyCPU causes SQLite invocation to fail with cryptic error
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
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.
It's supposed to copy both versions of the dll to the output directory: x86\sqlite3.dll and x64\sqlite3.dll.
This issue was moved to aspnet/Microsoft.Data.Sqlite#393
The issue is in the third-party SQLite package. The logic for building it is in natemcmaster/libsqlite3-package.