Microsoft.EntityFrameWork 中的 SQLite 加密 [英] SQLite encryption in Microsoft.EntityFrameWork

查看:25
本文介绍了Microsoft.EntityFrameWork 中的 SQLite 加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m currently trying to encrypt SQLite database with official SEE extension when using Microsoft entity framework.

我可以在使用 ADO.NET 时加密数据库.但是收到错误您在连接字符串中指定了密码,但本机 SQLite 库‘e_sqlite3’不支持加密"使用实体框架时.

I’m able to encrypt database when using ADO.NET. However getting an error "You specified a password in the connection string, but the native SQLite library ‘e_sqlite3’ doesn’t support encryption" when using entity frame work.

使用的 Nuget 包:

Nuget Packages Used:

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

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

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

Please can you advise how to fix this error with official SEE extension?

CustomDBContext.cs:

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;}

Program.cs:

Program.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));
}

推荐答案

要使用官方 SQLite 扩展进行加密,请选择实体框架直到 EF6,因为 System.Data.SQLite 库只支持到 EF6,没有直接支持用于 EF 内核.

To use official SQLite extension for encryption, please choose the entity framework until EF6 since the System.Data.SQLite library supports only till EF6 and there is no direct support for EF core.

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

If we still need to use entity framework core then use we can also consider other options like encryption using SQLCipher that supports EF core.

这篇关于Microsoft.EntityFrameWork 中的 SQLite 加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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