返回
EF报错System.Exception: You need to call SQLitePCL.raw.SetProvider().
2023-02-15 5238 0
使用EntityFrameworkCore.Sqlite时报错了,System.Exception: 'You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().'。
var scopeFactory = app.Services.GetRequiredService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ArticleContext>();
if (db.Database.EnsureCreated())
{
SeedData.Initialize(db);
}
}在执行到db.Database.EnsureCreated()是发生了异常:

检查了Nuget引用,发现项目安装了
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite.Core
这两个包。看上去不是代码的问题。
找到了解决方法是再安装Microsoft.EntityFrameworkCore.Sqlite包就可以了。

网友点评
提交
相关阅读
ASP.NET 使用Entity Framework (EF) 创建迁移修改SQLite数据库表结构
.NET Entity Framework(EF)高性能分页
EF Core 8 (EF8) Contains报错:Microsoft.Data.SqlClient.SqlException (0x80131904): 关键字 'WITH' 附近有语法错误。
EF Core预热处理优化
Entity Framework(EF) Core 10新特性全面解析:提升开发效率的关键更新
EF报错Win32Exception: 证书链是由不受信任的颁发机构颁发的。
EntityFramework(EF) SQLite常见问题和解决方案
Entity Framework Core 连接PostgreSQL