我目前正在尝试在使用 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));
}