"You need to call SQLitePCL.raw.SetProvider()" error when using Microsoft.EntityFrameworkCore.Sqlite 5.0.4 via C++\CLI wrapper

See original GitHub issue

I have a solution with the following structure:

  • DomainObjects - .NET 5.0 class library without any special dependencies, contains entities. Compiled as x86.
  • DAL (references DomainObjects) - .NET 5.0 class library which references Microsoft.EntityFrameworkCore.Sqlite 5.0.4 nuget package, contains DbContext and migrations (database is created at d:\test.db). Compiled as x86.
  • DAL.Test (references DAL) - .NET 5.0 console test application. Compiled as x86.
  • DAL.Interop (references DAL) - .NET 5.0 C++\CLI wrapper. Compiled as x86, Multi-byte Character Set, wchar_t-.
  • DAL.Interop.Test - C++ console test application. Compiled as x86, Multi-byte Character Set, wchar_t-.

DAL.Test runs without any problems, but DAL.Interop.Test throws an exception (inside DAL class library):

You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().

NB! If I switch from Microsoft.EntityFrameworkCore.Sqlite provider to Devart.Data.SQlite.EFCore provider - both test applications (.NET and C++) run without any problems.

NB2! If I remember correctly code was working with no problems with Microsoft.EntityFrameworkCore.Sqlite 3.1 on .NET Core 3.1.

Sample repo - https://github.com/bairog/EFCore5SQLiteCLRTest Compile it (for some reason you need to compile twice for compiling DAL.Interop.Test project) and start debugging DAL project (it starts DAL.Interop.Test.exe in project Debug settings)

Stack traces

Source: SQLitePCLRaw.core
You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().
   at SQLitePCL.raw.get_Provider()
   at SQLitePCL.raw.sqlite3_open_v2(utf8z filename, sqlite3& db, Int32 flags, utf8z vfs)
   at Microsoft.Data.Sqlite.SqliteConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrations()
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetAppliedMigrations(DatabaseFacade databaseFacade)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetPendingMigrations(DatabaseFacade databaseFacade)
   at DAL.CRUD.CreateDbContext() in D:\EFCore5SQLiteCLRTest\DAL\CRUD.cs:line 14

Provider and version information

EF Core version: 5.0.4 Database provider: Microsoft.EntityFrameworkCore.Sqlite Target framework: .NET 5.0 Operating system: Windows 10 x64 1909 IDE: Visual Studio 2019 16.9.2 REL Community

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:21 (8 by maintainers)

github_iconTop GitHub Comments

20reactions
bricelamcommented, Jul 31, 2022

@SoftCircuits I think you meant to use the Microsoft.EntityFrameworkCore.Sqlite package instead. The Core one is for those who want to provide their own native SQLite library.

- <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="6.0.7" />
+ <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.7" />
9reactions
woutervanranstcommented, Mar 25, 2023

I had erroneously added this package, which does not include the BCL <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="7.0.4" />

Adding this version instead fixed it for me <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.4" />

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I call SQLitePCL.Batteries.Init().?
I am attempting to create an SQLite database for my application and have come across this error. System.Exception: 'You need to call SQLitePCL....
Read more >
Entity Developer, Microsoft SQLite Driver, You need to call ...
So far so easy. When I then click to check database connection I get the followin error message: "You need to call SQLitePCL.raw.SetProvider()....
Read more >
MAUI EF Core 6 crash on IOS - Microsoft Q&A
Getting immediate crash during saving data into sqlite. using following release parameters <PropertyGroup ...
Read more >
How do I call SQLitePCL.Batteries.Init().?-Entity Framework
Core package. You should install and use the Microsoft.EntityFrameworkCore.Sqlite package instead, which has a dependency upon the SQLitePCLRaw package.
Read more >
C# – How to call SQLitePCL.Batteries.Init().
I am attempting to create an SQLite database for my application and have come across this error. System.Exception: 'You need to call SQLitePCL.raw.SetProvider() ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found