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


    }
}
相关推荐
Austindatabases1 天前
数据不准确,数据丢失,SQLite怎么保证计算不丢数--SQLite 五脏俱全系列 (5)
java·开发语言·数据库·sqlite
张祥前世界大同1 天前
基于矢量光速螺旋时空的真空介电常数与引力常数统一关系(最终定稿版)
大数据·sqlite
北冥有羽Victoria2 天前
Django中间件实战:FBV/CBV日志全兼容
数据库·vscode·后端·python·django·sqlite·开源
夏沫琅琊2 天前
Mac 上用 Homebrew 安装 DB Browser for SQLite 时,被 Homebrew 自动更新卡住了
数据库·macos·sqlite
雨浓YN2 天前
GKTGD 工业监控系统-04MySQL 与 SQLite 数据库对比(类库:NET8_SQLData)
数据库·sqlite·wpf
kobe_OKOK_5 天前
DRF 搜索功能实现
数据库·sqlite
dblens 数据库管理和开发工具5 天前
SQLite 不该只有“打开表格”,它也需要一个 Agent 工作台
数据库·sqlite
dblens 数据库管理和开发工具5 天前
我给 SQLite 做了一个带 Agent 的桌面工具
数据库·sqlite
万事大吉CC6 天前
【7】Django 类视图实战指南:如何高效引用与配置
数据库·sqlite
木土雨成小小测试员6 天前
Python测试开发之后端二完结
数据库·python·sqlite