如何调用SQLitePCL.Batteries.Init(). [英] How do I call SQLitePCL.Batteries.Init().?

查看:109
本文介绍了如何调用SQLitePCL.Batteries.Init().的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用程序创建一个SQLite数据库,并且遇到了此错误.

System.Exception:'您需要调用SQLitePCL.raw.SetProvider().如果您正在使用捆绑包,这是通过调用SQLitePCL.Batteries.Init().'

我创建了一个简单的控制台应用程序,可以运行完全相同的代码进行创建,没有任何问题.代码看起来像这样!

 使用(var dataContext = new SampleDBContext()){dataContext.Accounts.Add(new Account(){AccountName = name,AccountBalance = balance});}公共类SampleDBContext:DbContext{私人静态布尔值_created = false;公共SampleDBContext(){如果(!_created){_created = true;Database.EnsureDeleted();Database.EnsureCreated();}}受保护的重写void OnConfiguring(DbContextOptionsBuilder optionbuilder){optionbuilder.UseSqlite(@数据源="源文件夹"\ Database.db");}公共DbSet< Account>帐户{get;放;}} 

有人可以阐明这个问题吗?我在两个项目上都安装了相同的Nuget软件包,两者之间的唯一区别是用于数据库的数据源和POCO类.

谢谢.

修改我的程序当前由引用 .Net Framework类库控制台应用程序组成. Console应用程序简单有一个如下所示的构造函数:

 公共程序(){使用(var db = new FinancialContext()){db.Accounts.Add(new Account(){AccountName ="RBS",AccountBalance = 20});}} 

类库具有如下的FinancialContext:

 公共类FinancialContext:DbContext{公共DbSet< Account>帐户{get;放;}公共FinancialContext(){#Database.EnsureDeleted();Database.EnsureCreated();}受保护的重写void OnConfiguring(DbContextOptionsBuilder optionbuilder){optionbuilder.UseSqlite(@"Data Source ="某些源文件夹"\ Database.db");}} 

上述错误显示在#符号点处,我的编码方式是否存在问题?我真的很想知道问题是什么,所以我可以正确地解决它,而不是应用修复".我也尝试了注释中的建议,但是将代码行 SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3()); 放在 Console Application 中给出了错误 SQLitePCL 不在当前上下文中,这让我以为我缺少参考?

解决方案

当我尝试避免任何其他依赖关系并使用 Microsoft.EntityFrameworkCore.Sqlite.Core 包时,这发生在我身上./p>

您应该改为安装并使用 Microsoft.EntityFrameworkCore.Sqlite 软件包,该软件包依赖于 SQLitePCLRaw 软件包.

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?

解决方案

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.

这篇关于如何调用SQLitePCL.Batteries.Init().的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆