sqlite.net PCL throws TypeInitalizationException
Asked Answered
B

1

7

We changed from sqlite-net to sqlite.net PCL edition. Our project worked fine with the old one - with the PCL edition, we now have to explicitely declare our platform by using.

var conn = new SQLiteConnection(new SQLitePlatformWin32(),  Path.Combine(folderPath, dbName));

But now, when I call this line, we get a

A first chance exception of type 'System.TypeInitializationException' occurred in SQLite.Net.Platform.Win32.dll

Additional information: The type initializer for "SQLite.Net.Platform.Win32.SQLiteApiWin32Internal" threw an exception.

The inner Exception tells us

{"Failed to load native sqlite library"}

Stacktrace:

at SQLite.Net.Platform.Win32.SQLiteApiWin32Internal.sqlite3_open_v2(Byte[] filename, IntPtr& db, Int32 flags, IntPtr zvfs)

at SQLite.Net.Platform.Win32.SQLiteApiWin32.Open(Byte[] filename, IDbHandle& db, Int32 flags, IntPtr zvfs)

at SQLite.Net.SQLiteConnection..ctor(ISQLitePlatform sqlitePlatform, String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks, IBlobSerializer serializer, IDictionary2 tableMappings, IDictionary2 extraTypeMappings, IContractResolver resolver)

at SQLite.Net.SQLiteConnection..ctor(ISQLitePlatform sqlitePlatform, String databasePath, Boolean storeDateTimeAsTicks, IBlobSerializer serializer, IDictionary2 tableMappings, IDictionary2 extraTypeMappings, IContractResolver resolver)

However the sqlite3.dll is in the application folder and it actually did work with the unforked version. I played around with compiling for x86 and x64 and downloaded the latest sqlite3.dll for Win32 but all without success. What are we missing?

Bloodthirsty answered 22/5, 2015 at 14:20 Comment(0)
L
18

I had the same problem in a tests project. Solved it by adding the package http://www.nuget.org/packages/System.Data.SQLite.Core for the SQLitePlatformWin32.

Although not listed as a dependency in the actual version, it was in previous one of the platform's nuget package (http://www.nuget.org/packages/SQLite.Net.Platform.Win32/)

Additionally: in case of using SQLitePlatformGeneric a copy of sqlite3.dll should be placed in the output directory.

Loathly answered 26/5, 2015 at 19:8 Comment(4)
This was the solution - it was the interop dll files that were missing and after adding the core package to my project it also added those files. Interestingly I didn't have to add the files as a direct dependency in my class lib project (where I had the sqlite pcl files) but to my gui project which is dependend on the class lib to get it to work.Bloodthirsty
Make sure you pick the correct .Core package (x86/x64), not the MSIL .Core package.Cambrai
what is the difference between SQLitePlatformWin32 and SQLitePlatformGeneric?Linolinocut
@batmaci From the readme on the platform's git repo github.com/oysteinkrog/SQLite.Net-PCL#sqliteconnection "Win32 (bundles sqlite binaries for windows, works on both x86 and x64 automatically)" "Generic (net4 project without any sqlite3 binaries, requires sqlite installed in the OS)"Loathly

© 2022 - 2024 — McMap. All rights reserved.