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


    }
}
相关推荐
mit6.8244 小时前
SQLite的优雅设计
sqlite
Access开发易登软件1 天前
Access 用 VBA 操作 SQLite,不用装任何驱动
jvm·数据库·sqlite·vba·access·access开发
谷谷地图下载器1 天前
全球、台湾省的无水印·街景数据(离线数据),专为可视化项目定制,支持国产化
javascript·c++·3d·arcgis·sqlite
彭祥.1 天前
基于SQLite与face_recognition的人脸库管理
数据库·计算机视觉·sqlite
搜佛说1 天前
sfsDb 和 SQLite、InfluxDB “硬碰硬”的底层性能与技术架构对比
jvm·架构·sqlite
星越华夏1 天前
SQLite数据库优化实战技巧案例
数据库·sqlite
abcy0712132 天前
python在models定义了一个对象,接口调用时报错对象不存在models.xx.DoesNotExist
数据库·sqlite
ggabb2 天前
形制与意蕴的殊途:中英诗歌的优劣差异探析
sqlite
Dxy12393102162 天前
Django 三种 ENGINE 的区别
python·django·sqlite
abcy0712132 天前
django聚合函数
数据库·sqlite