1

我目前正在尝试在使用 Microsoft 实体框架时使用官方SEE扩展来加密 SQLite 数据库。

使用 ADO.NET 时,我能够加密数据库。但是在使用实体框架时出现错误“您在连接字符串中指定了密码,但本机 SQLite 库 'e_sqlite3' 不支持加密”。

使用的 Nuget 包:

[Microsoft.EntityFrameWork.Core Microsoft.EntityFrameWork.Core.SQLite SQLite.Encryption.Extension System.Data.SQLite.Core]

请您建议如何使用官方 SEE 扩展来修复此错误?

CustomDBContext.cs:

private readonly bool _created = false;

public CustomDbContext(DBContextOptions<CustomDbContext> options):base(options){

if(!_created)
{
_created = true;
Database.EnsureCreated();
}
}

public DbSet<SampleEntity> SampleEntities {get; set;}

程序.cs:

static void Main(string[] args)
{
var services = new ServiceCollection();
ConfigureService(services);
using ServiceProvider provider = services.BuildServiceProvider();
provider.GetService<ICustomDBContext>();
}

private static void ConfigureServices(ServiceCollection services)
{
string password = Convert.ToHexString(Encoding.Default.GetBytes("aes256:test");

SQLiteCommand.Execute("PRAGMA activate_extensions='see-7bb07b8d471d642e'", SQLiteExecuteType.NonQuery,@"Data Source=c:\users\test.db");

SQLiteConnectionStringBuilder connectionStringBuilder = new(){
ConnectionString = @"Data Source=c:\users\test.db;Password="+password};

SQLiteConnection conn = new(connectionStringBuilder.ConnectionString);

connection.Open();
connection.ChangePassword(password);
services.AddDbContext<CustomDBContext>(options => options.UseSqlite(connection));
}
4

1 回答 1

0

要使用官方 SQLite 扩展进行加密,请选择 EF6 之前的实体框架,因为 System.Data.SQLite 库仅支持 EF6,并且没有直接支持 EF 内核。

如果我们仍然需要使用实体框架核心,那么我们还可以考虑其他选项,例如使用支持 EF 核心的 SQLCipher 进行加密。

于 2021-07-27T10:42:56.780 回答