返回

EF报错System.Exception: You need to call SQLitePCL.raw.SetProvider().

2023-02-15 EF EntityFrameworkCore.Sqlite System.Exception SQLitePCL.raw.SetProvider() 报错 4254 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()是发生了异常:

EF报错System.Exception: You need to call SQLitePCL.raw.SetProvider().

检查了Nuget引用,发现项目安装了

Microsoft.EntityFrameworkCore

Microsoft.EntityFrameworkCore.Sqlite.Core

这两个包。看上去不是代码的问题。

找到了解决方法是再安装Microsoft.EntityFrameworkCore.Sqlite包就可以了。

EF报错System.Exception: You need to call SQLitePCL.raw.SetProvider().

顶部