简体   繁体   中英

Cannot run SQLite.net PCL in ASP.NET MVC

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?

这可以通过安装nuGet包Microsoft.EntityFrameworkCore.Sqlite来修复(注意,不是Microsoft.EntityFrameworkCore.Sqlite.Core)

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

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.

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);
            }
        }

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. 这张图片详细阐述了问题

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM