Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.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?

like image 391
Tristan Trainer Avatar asked Jun 07 '18 16:06

Tristan Trainer


4 Answers

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

like image 30
Stepan Ivanenko Avatar answered Nov 17 '22 08:11

Stepan Ivanenko


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.

like image 122
René Schindhelm Avatar answered Nov 17 '22 09:11

René Schindhelm


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.

like image 7
Varus Septimus Avatar answered Nov 17 '22 10:11

Varus Septimus


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

like image 5
Pedro Paredes Avatar answered Nov 17 '22 08:11

Pedro Paredes