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


    }
}
相关推荐
百锦再12 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
kangzerun13 天前
SQLiteManager:一个优雅的Qt SQLite数据库操作类
数据库·qt·sqlite
用户57573033462413 天前
AIGC 时代数据库革命:告别手写 SQL,用自然语言驾驭 SQLite
sqlite
GDAL14 天前
better-sqlite3 深度教程:为什么它比 node‑sqlite3 更好、怎么用、什么时候不该用
sqlite·better-sqlite3
GDAL14 天前
SQLite 与 MySQL 性能深度对比:场景决定最优解
数据库·mysql·sqlite
钱彬 (Qian Bin)15 天前
FastAPI的Alembic踩坑记录:缺失历史迁移脚本如何保留数据重建版本控制
sqlite·fastapi·数据库迁移·alembic
qq_4542450316 天前
GraphMindStudio 数据操作层解析:基于 SQLite 的封装与自动化存储
sqlite·c#
喵手16 天前
Python爬虫实战:Boss直聘职位数据采集实战 - Playwright + 结构化解析完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·sqlite·爬虫实战·playwright·boss直聘职位数据采集·结构化解析
GDAL17 天前
SQLite 的适用场景与选型指南:它不是轻量 MySQL,而是「文件的升级版」
数据库·mysql·sqlite
布局呆星17 天前
Python 入门:FastAPI + SQLite3 + Requests 基础教学
python·sqlite·fastapi