4

I am getting my feet wet with Frank A. Krueger's SQLite.net PCL. I am attempting to use it as the common data layer for an Android app as well as an ASP.NET MVC web application. I have had some success with Android use, but in trying to use it for the web app I get the following exception:

You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().

So, I call this Init function in my controller's constructor, right along with the database initialization:

SQLitePCL.Batteries.Init();

var dbPath = Path.Combine(Constants.DataDir, "SdgData.sqlite3");
var sqliteConnection = new SQLiteConnection(dbPath);
var sdgSqlRepository = new SdgSqlRepository(sqliteConnection);
crm = new CrmManager(sdgSqlRepository);

However, it seems as though the Init isn't even being called - I set a breakpoint on it as well as the next line with the call to Combine, and the second is hit without the first even being noticed. (Possibly because the Init is a PCL library call not applicable to the ASP.NET platform?)

Is there a way to run SQLite.net PCL in an ASP.NET web application, or am I going to have to look for a different data source?

2
  • which SQLitePCL project are you using? There are way too many out there. Commented Nov 4, 2016 at 1:54
  • Frank A. Krueger - github.com/praeclarum/sqlite-net Commented Nov 4, 2016 at 2:08

6 Answers 6

3

This can be fixed by installing the nuGet package Microsoft.EntityFrameworkCore.Sqlite (beware, not Microsoft.EntityFrameworkCore.Sqlite.Core)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, didn't work for me. Within the context of this question I was using PCL targeting an incompatible framework. I got the following error attempting to install what I am guessing is the version to which you were referring (2.1.4, latest as of your answer): Could not install package 'Microsoft.EntityFrameworkCore.Sqlite 2.1.4'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
1

I have the same issue I fix it by import

SQLitePCLRaw.bundle_e_sqlite3
SQLitePCLRaw.bundle_sqlcipher
SQLitePCLRaw.bundle_green

reference

https://github.com/ericsink/SQLitePCL.raw/wiki/SQLitePCL.Batteries.Init

Comments

1

Current solution i found is that i have to call it like below ...

Although i am not happy with the solution and i want to do a deep dig but moving forward with this solution for now -

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string dbPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);


            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlite($@"Data Source={dbPath}\sqlite\.sample.sqlite");
                Batteries.Init();
                base.OnConfiguring(optionsBuilder);
            }
        }

2 Comments

Unbelievable; that fixed it for me. I don't understand why that is necessary though?
there are various reason behind this, also you must see another follow up answer which i have posted below because later i jumped into more difficulties. What i learned during this was its the nuget packages which cause this issue. There are some of them and we need to use the right one. Also remember that everytime a request was made to my APIs i never got optionsBuilder.IsConfigured as true so the code inside if condition keep executing all the time. Something i will tweak later. Moreover it sqllite version also matter. I was on V2 i guess.
1

I eventually got this to work by replacing

SQLitePCL.Batteries.Init();

with

SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());

(See https://github.com/ericsink/SQLitePCL.raw/wiki/SQLitePCL.Batteries.Init)

However, I recently checked out the old code, attempting to retrace my steps to verify some of the previous answers. I got the following error:

System.DllNotFoundException -- Unable to load DLL 'e_sqlite3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Looking around some (e.g. https://github.com/ErikEJ/SqlCeToolbox/issues/599), it seems the solution to this issue was to simply update sqlite-net. I updated to the latest stable version (1.5.231, was on 1.2.0), and both the original call to Init as well as my previous fix with SetProvider work as expected.

Comments

0

Have you tried adding one of the SQLitePCLRaw bundles to your project?

I discovered that this was required when building a UWP project that referenced a Portable project that itself referenced SQLite-net. Both projects in the solution needed a SQLitePCLRaw bundle in order for the call to Batteries.Init() to be executed.

There is a clue to this setup in the exception message.

Comments

0

May be my above answer will help someone as process started working

Later found more issues that there were few Dlls which not present in bin folder of primary project in our case a window application bin folder

I was WPF and WCF projects so Dlls were always copied to WCF output not to WPF output

To overcome the problem I copied x64, x86 folders containing e_sqlite3.dll binaries to the project root Now went to Visual studio and clicked the file inside x64, x86 folder and chosen Copy to Output Directory Copy if newer

Binaries which were missing are - e_sqlite3.dll

See attached screenshot for more details. This image elaborates more regarding issue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.