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


    }
}
相关推荐
H2122021652 天前
SQLite3库链接与加载问题解决方案
数据库·oracle·sqlite
傻啦嘿哟8 天前
Django模型开发全解析:字段、元数据与继承的实战指南
数据库·sqlite
春夜喜雨9 天前
sqlite的sql语法与技术架构研究
sqlite
囚~徒~10 天前
uwsgi 启动 django 服务
python·django·sqlite
路由侠内网穿透11 天前
本地部署 SQLite 数据库管理工具 SQLite Browser ( Web ) 并实现外部访问
运维·服务器·开发语言·前端·数据库·sqlite
l1t11 天前
利用DeepSeek改写并增强测试Duckdb和sqlite的不同插入方法性能
python·sql·sqlite·duckdb
践行见远11 天前
django之中间件
中间件·django·sqlite
就是有点傻13 天前
如何使用EF框架操作Sqlite
数据库·sqlite
goodmao13 天前
【mdx】英语字典数据库转sqlite
数据库·sqlite·mdx·英语词典
你的人类朋友14 天前
❤️‍🔥为了省内存选择sqlite,代价是什么
数据库·后端·sqlite