56

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(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().'

I created a simple console app the run the exact same code for creation, with no issues. The code looks like this!

using (var dataContext = new SampleDBContext())
{
    dataContext.Accounts.Add(new Account() { AccountName = name, AccountBalance = balance });
}


public class SampleDBContext : DbContext
{
    private static bool _created = false;
    public SampleDBContext()
    {
        if (!_created)
        {
            _created = true;
            Database.EnsureDeleted();
            Database.EnsureCreated();
        }
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionbuilder)
    {
        optionbuilder.UseSqlite(@"Data Source="Source folder"\Database.db");
    }

    public DbSet<Account> Accounts { get; set; }
}

Can anyone shed any light on the issue? I installed the same Nuget Packages on both projects, the only difference between the two is the Data Source and the POCO classes I used for the database.

Thanks.

Edit My program currently consists of a Console application that references a .Net Framework Class Library. The Console application simple has a constructor that looks like this:

public Program()
{   
    using (var db = new FinancialContext())
    {
        db.Accounts.Add(new Account() { AccountName = "RBS", AccountBalance=20 });
    }
}

The Class Library has a FinancialContext as Follows:

public class FinancialContext : DbContext
{
    public DbSet<Account> Accounts { get; set; }

    public FinancialContext()
    {
      # Database.EnsureDeleted();
        Database.EnsureCreated();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionbuilder)
    {
        optionbuilder.UseSqlite(@"Data Source="Some Source Folder"\Database.db");
    }
}

The Above error is shown at the # symbol point, is there a problem with the way I am coding? I would really like to know what the issue is so I can fix it properly rather than apply a 'fix'. Also I tried the suggestion in the comments, but putting the code line SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3()); in the Console Application gave the error SQLitePCL is not in the current context, which leaves me thinking I am missing a reference?

Tristan Trainer
  • 2,458
  • 2
  • 12
  • 31
  • https://forums.xamarin.com/discussion/88434/if-you-are-using-a-bundle-package-this-is-done-by-calling-sqlitepcl-batteries-init – stuartd Jun 07 '18 at 16:57
  • Thank you Stuart, I saw this when searching for an answer, however this doesn't really say how you do this. I am creating a WPF application, would I enter this code in the OnStartup method? It's not really clear what this is doing really, just that someone placed a line of code somewhere in their application. Thanks in advance! – Tristan Trainer Jun 07 '18 at 21:37
  • I have edited the question with further comments, might possible add more detail to the question. – Tristan Trainer Jun 07 '18 at 22:21

8 Answers8

85

This happened to me when I tried to avoid any additional dependencies and went for the Microsoft.EntityFrameworkCore.Sqlite.Core package.

You should install and use the Microsoft.EntityFrameworkCore.Sqlite package instead, which has a dependency upon the SQLitePCLRaw package.

René Schindhelm
  • 1,187
  • 1
  • 10
  • 15
22

Install Nuget Package Microsoft.Data.Sqlite (not Microsoft.Data.Sqlite.Core). (my version is 2.2.2)

and use SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());

 connection = new SqliteConnection("Data Source = Sample.db");

 SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());

 connection.Open();

but I advise use nuget package System.Data.SQLite instead Microsoft.Data.Sqlite

Stepan Ivanenko
  • 851
  • 9
  • 18
  • Your package name(s, depending on how you want to count them) are wrong. They should be `Microsoft.Data.Sqlite` **NOT** `Mictosoft.Data.Sqlite`. – Professor Tom Apr 21 '19 at 17:55
  • 1
    "I advise use nuget package System.Data.SQLite instead Microsoft.Data.Sqlite" Why? I just had to switch from that to this because of this issue https://stackoverflow.com/questions/13028069/unable-to-load-dll-sqlite-interop-dll and none of the solutions there worked well for me. "SQLitePCL.raw.SetProvider" This was not needed for me (perhaps something changed since this solution was posted. – willbush Dec 03 '20 at 20:02
7

I had this very exact error. It turned out that I had package Microsoft.Data.Sqlite.Core (2.2.4) installed, but not SQLitePCLRaw.bundle_winsqlite3.

Installing package SQLitePCLRaw.bundle_winsqlite3 (1.1.13) solved the issue.

Varus Septimus
  • 1,342
  • 18
  • 12
4

I got this issue when working with Microsoft.EntityFrameworkCore.Sqlite version 3.1.10. The above solutions did not work for me. Then I have modified the My DbContext as follows (added SQLitePCL.Batteries.Init(); to OnConfiguring method) and the issue is gone!!!

public class ApplicationDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source=mydb.db");
        SQLitePCL.Batteries.Init();
    }
}
4

Switching from Microsoft.Data.Sqlite.Core to Microsoft.Data.Sqlite as Patrick said here did the trick for me

Pedro Paredes
  • 590
  • 6
  • 5
1

For some reason the Nuget Package hadn't installed the required references, reinstalled the package and it has corrected the issue!

Missing the SQLitePCL.raw* references.

Tristan Trainer
  • 2,458
  • 2
  • 12
  • 31
  • You mean your issue was resolved by re-installing the package - and not by the other suggested resolutions, correct? I am asking because I have a similar issue where it's complaining: `Could not load file or assembly 'SQLitePCLRaw.core`. Like yourself, I'm also using `EF` (and not `EF Core`. – nam May 31 '20 at 18:44
  • Yes that's correct nam - I just had to do a proper reinstall of the package and it worked - this was over two years ago but I'm guessing the same issue would apply :) – Tristan Trainer Jun 03 '20 at 23:51
0

I had the same issue when I try to use, Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.6". What I did was downgrade the version into 2.2.2 which I was previously used. Then issue not occur.

Wikum
  • 1
  • 1
0

On Xamarin.iOs I had the same problem.

Solution: Call SQLitePCL.Batteries_V2.Init() In the FinishedLaunching method of your AppDelegate class.

Source: https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/xamarin

Akli
  • 1,225
  • 1
  • 15
  • 27