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


    }
}
相关推荐
Sammyyyyy21 小时前
Django 6.0 发布,新增原生任务队列与 CSP 支持
数据库·后端·python·django·sqlite·servbay
赤龙绕月2 天前
SQLite NET
数据库·sqlite
透明的玻璃杯3 天前
sqlite数据库链接池二
数据库·oracle·sqlite
透明的玻璃杯3 天前
sqlite数据库连接池
jvm·数据库·sqlite
越甲八千4 天前
ASGI和AWSIG区别
数据库·python·sqlite
码农12138号4 天前
Bugku 2023 HackINI Virtual Shop 和 2023 HackINI Virtual Shop 2
web安全·sqlite·sql注入
飞Link5 天前
【Django】Django 调用外部 Python 程序的完整指南
后端·python·django·sqlite
姜太小白6 天前
【数据库】SQLite 时间加1天的方法总结
java·数据库·sqlite
离别又见离别6 天前
electron-vite桌面端better-sqlite3跨平台兼容问题
javascript·electron·sqlite
交流QQ:4877392787 天前
基于YOLOv9的深度学习道路缺陷检测技术:融合DCNv4、自研BSAM注意力与自适应阈值焦点...
sqlite