带有 Entity Framework Core 的密码保护 SQLite [英] Password protected SQLite with Entity Framework Core

查看:39
本文介绍了带有 Entity Framework Core 的密码保护 SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个受密码保护的 SQLite 数据库,以便在使用 Entity Framework Core 的 WPF 应用程序中使用它.

I am trying to create a password protected SQLite database, in order to used it within a WPF application using Entity Framework Core.

我了解了如何从现有的 SQLite 数据库(数据库优先方法)生成我的 DbContext 和实体,但我无法使用受密码保护的数据库.

I find out how to generate my DbContext and Entities from an existing SQLite DB (database first approach) but I can't get it working with password protected DB.

实际上,我什至不确定如何创建受密码保护的 SQLite DB.加密数据库和受密码保护的数据库有什么区别?

Actually, I am not even sure of how to create a password protected SQLite DB. What is the difference between an encrypted db and a password protected db ?

推荐答案

根据这个 文章 和这个issue,没有使用 Microsoft.Data.Sqlite 程序集(由 EF Core 使用)加密数据库的方式(还?).

According to this article and this issue, there is no way (yet?) to encrypt the database using the Microsoft.Data.Sqlite assembly (used by EF Core).

基于此,这是我为使其与 EF Core 一起工作所做的工作:

Based on this, here is what I've done to get it working with EF Core:

  • System.Data.SQLite.Core 包添加到项目
  • 在配置你的 dbContext 时,给 optionsBuilder 你自己的 DbConnection:

  • add the System.Data.SQLite.Core package to the project
  • while configuring your dbContext, give the optionsBuilder your own DbConnection:

var conn = new SQLiteConnection(@"Data Source=yourSQLite.db;");
conn.Open();

var command = conn.CreateCommand();
command.CommandText = "PRAGMA key = password;";
command.ExecuteNonQuery();

optionsBuilder.UseSqlite(conn);

使用 System.Data.SQLite.Core 程序集中的 SQLiteConnection(可以管理加密数据库)非常重要,而不是 SqliteConnection 来自 Microsoft.Data.Sqlite.

It is very important to use the SQLiteConnection (which can manage encrypted database) from the System.Data.SQLite.Core assembly and not the SqliteConnection from Microsoft.Data.Sqlite.

根据文章,您可以通过替换 Microsoft.Data.Sqlite 中附带的 sqlite3.dll 来使用内置的 SqliteConnection> 由另一个处理加密数据库的人组装(你可以在这个 repo 上找到一个免费的).但我没有测试过.

According to the article, you could probably used the built in SqliteConnection by replacing the sqlite3.dll shipped within the Microsoft.Data.Sqlite assembly by another one that handle encrypted database (you can find a free one on this repo). But I did not tested it.

这里是如何做到这一点!

这篇关于带有 Entity Framework Core 的密码保护 SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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