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


    }
}
相关推荐
somi71 小时前
ARM-10-SQLite3 库移植笔记
jvm·笔记·sqlite
misL NITL2 小时前
数据库操作与数据管理——Rust 与 SQLite 的集成
数据库·rust·sqlite
yuanpan1 天前
Python 连接 SQLite 数据库:从建表到增删改查的完整演示项目
数据库·python·sqlite
ILYT NCTR1 天前
vscode配置django环境并创建django项目(全图文操作)
vscode·django·sqlite
Deitymoon2 天前
嵌入式数据库——SQLite基础
数据库·sqlite
天选之子1232 天前
Django基本概念入门(一)
python·django·sqlite
天选之子1232 天前
Django模板(二)
数据库·django·sqlite
观无3 天前
FastAPI + SQLite 原生无主键表 完整增删改查
数据库·sqlite·fastapi
dLYG DUMS4 天前
vscode配置django环境并创建django项目(全图文操作)
vscode·django·sqlite
l1t4 天前
DeepSeek总结的致力于在一分钟内将十亿行数据插入 SQLite
python·sqlite