简体   繁体   English

无法在ASP.NET MVC中运行SQLite.net PCL

[英]Cannot run SQLite.net PCL in ASP.NET MVC

I am getting my feet wet with Frank A. Krueger's SQLite.net PCL . 我正在和Frank A. Krueger的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. 我试图将它用作Android应用程序和ASP.NET MVC Web应用程序的公共数据层。 I have had some success with Android use, but in trying to use it for the web app I get the following exception: 我在使用Android方面取得了一些成功,但在尝试将其用于Web应用程序时,我得到以下异常:

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: 所以,我在控制器的构造函数中调用此Init函数,以及数据库初始化:

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. 然而,好像Init甚至没有被调用 - 我在它上面设置了一个断点以及调用Combine的下一行,而第二行是在没有第一个被注意的情况下被击中。 (Possibly because the Init is a PCL library call not applicable to the ASP.NET platform?) (可能是因为Init是PCL库调用不适用于ASP.NET平台?)

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? 有没有办法在ASP.NET Web应用程序中运行SQLite.net PCL,或者我将不得不寻找不同的数据源?

这可以通过安装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 https://github.com/ericsink/SQLitePCL.raw/wiki/SQLitePCL.Batteries.Init

Have you tried adding one of the SQLitePCLRaw bundles to your project? 您是否尝试过将SQLitePCLRaw包中的一个添加到项目中?

I discovered that this was required when building a UWP project that referenced a Portable project that itself referenced SQLite-net. 我发现在构建引用了一个本身引用SQLite-net的Portable项目的UWP项目时,这是必需的。 Both projects in the solution needed a SQLitePCLRaw bundle in order for the call to Batteries.Init() to be executed. 解决方案中的两个项目都需要一个SQLitePCLRaw包,以便执行对Batteries.Init()的调用。

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 后来发现更多的问题,在我们的案例中,一个窗口应用程序bin文件夹中几乎没有Dll存在于主项目的bin文件夹中

I was WPF and WCF projects so Dlls were always copied to WCF output not to WPF output 我是WPF和WCF项目,所以Dlls总是被复制到WCF输出而不是WPF输出

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 为了解决这个问题,我将包含e_sqlite3.dll二进制文件的x64,x86文件夹复制到项目根目录,然后转到Visual Studio并单击x64,x86文件夹中的文件并选择Copy to Output Directory Copy if newer

Binaries which were missing are - e_sqlite3.dll 缺少的二进制文件是 - 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 ) (参见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. 看一些(例如https://github.com/ErikEJ/SqlCeToolbox/issues/599 ),似乎这个问题的解决方案是简单地更新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. 我更新到最新的稳定版本(1.5.231,在1.2.0),并且对Init的原始调用以及我之前使用SetProvider修复SetProvider按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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