无法在 SQLite 的连接字符串中提供密码 [英] Cannot provide password in Connection String for SQLite

查看:35
本文介绍了无法在 SQLite 的连接字符串中提供密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 SQLite 与实体框架一起使用.

我使用以下代码创建数据库:

class MyContext : DbContext{//DbSets、OnModelCreating() 等protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseSqlite("数据源=c:\test.db");}}//这是在我的测试方法中使用 (var db = new MyContext()){db.Database.EnsureCreated();}

以上代码有效.数据库被创建.但我想通过在连接字符串中提供密码来加密数据库:

optionsBuilder.UseSqlite("Data Source=c:\test.db;Password=mypassword");

在这种情况下,在调用 EnsureCreated 后,我得到异常

<块引用>

System.Data.dll 中发生类型为System.ArgumentException"的未处理异常附加信息:不支持关键字:'password'.

使用密码有什么问题?如何加密该 SQLite 数据库?

解决方案

根据这个答案 保护 EntityFramework 核心应用程序使用的 SQLite 数据库

<块引用>

EF Core 使用 Microsoft.Data.Sqlite,目前不支持开箱即用的加密.看https://github.com/aspnet/Microsoft.Data.Sqlite/issues/184.你可以使用 SQLite 扩展自己添加加密.

在提供的 GitHub 链接上还有其他链接到

Microsoft.Data.Sqlite 中的加密

I use SQLite with Entity Framework.

I create the DB with following code:

class MyContext : DbContext
{   
    // DbSets, OnModelCreating(), etc.

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source=c:\test.db");
    }
}

//this is in my test method
 using (var db = new MyContext())
{
    db.Database.EnsureCreated();
}

The above code works. The DB gets created. But I want to encrypt the DB by providing password in connection string:

optionsBuilder.UseSqlite("Data Source=c:\test.db;Password=mypassword");

In this case, after EnsureCreated is called, I get exception

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Keyword not supported: 'password'.

What is wrong with using password? How can I encrypt that SQLite database?

解决方案

According to this answer Protect SQLite database used by EntityFramework Core Application

EF Core uses Microsoft.Data.Sqlite, which does not currently support encryption out-of-box. See https://github.com/aspnet/Microsoft.Data.Sqlite/issues/184. You could add encryption yourself using SQLite extensions.

At the GitHub link provided there were other links to an alternative at

Encryption in Microsoft.Data.Sqlite

这篇关于无法在 SQLite 的连接字符串中提供密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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