You need to call SQLitePCL.raw.SetProvider()

在.NET环境中使用Entity Framework Core(EF Core)连接SQLite数据库时,报错。

使用框架 .NET8

错误信息:

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

这个错误主要原因就是没有安装nuget包: SQLitePCLRaw.bundle_e_sqlite3

需要安装包:
SQLitePCLRaw.bundle_e_sqlite3,版本 2.1.10
Microsoft.EntityFrameworkCore.Sqlite.Core,版本 9.0.0

ef core上下文参考代码:

csharp 复制代码
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System.Reflection.Metadata;
using WpfRepairProject.Model;


namespace WpfRepairProject.IData_impl
{
    public class EFContextSQLite : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            try
            { 
               string  filePath = "G:\\DataBase\\ljRepaireProject.sqlite"; 
                options.UseSqlite($"Data Source={filePath}");             

                //设置不跟踪所有查询  
                options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);


#if DEBUG
                //启用敏感数据日志记录
                options.EnableSensitiveDataLogging();

                //记录日志          
                options.LogTo(msg =>
                {
                    //调试-窗口消息
                    System.Diagnostics.Debug.WriteLine(msg);

                    //输出-窗口消息
                    Console.WriteLine(msg);
 
                }, Microsoft.Extensions.Logging.LogLevel.Information);
#endif
            }
            catch (Exception ex)
            {
                ex = ex.InnerException ?? ex;
                //LogHelpter.AddLog("数据库连接异常," + ex.Message, "error_DbContext");
            }
        }

        public DbSet<K_department> K_department { get; set; }


    }
}
相关推荐
代码程序猿RIP13 小时前
【SQLite 库】sqlite3_open_v2
jvm·oracle·sqlite
java_python源码1 天前
python高校心理健康服务小程序(源码+文档+调试+基础修改+答疑)
数据库·sqlite
shixian10304112 天前
Django 学习日志
数据库·学习·sqlite
yzx9910133 天前
Django 配置与安装完整指南
数据库·django·sqlite
数据知道4 天前
Go语言:用Go操作SQLite详解
开发语言·后端·golang·sqlite·go语言
hqwest4 天前
QT肝8天16--加载动态菜单
开发语言·数据库·qt·ui·sqlite
疯狂的Alex7 天前
【C#避坑实战系列文章15】C# WinForm 上位机开发:解决串口粘包+LiveCharts卡顿+InfluxDB存储(免费代码+仿真工具)
sqlite·c#·上位机·串口通信·livechars·c#硬件对接
奔跑吧邓邓子7 天前
【C++实战(64)】C++ 邂逅SQLite3:数据库编程实战之旅
数据库·c++·sqlite·实战·sqlite3·数据库编程
hqwest9 天前
QT肝8天07--连接数据库
开发语言·数据库·c++·qt·sqlite·上位机·qt开发
专注VB编程开发20年10 天前
VB6 ADO没有轻量级内存数据库吗?类似SQLITE
sqlite·ado·内存数据库·内存表