[英]How can I run a migration with Entity Framework Core on an encrypted SQLite database?
I have an ASP.NET Core console application (targeting version 2.2), using Entity Framework Core.我有一个使用 Entity Framework Core 的 ASP.NET Core 控制台应用程序(针对版本 2.2)。 I'm trying to connect to an empty, encrypted SQLite database created with DB Browser for SQLCipher.
我正在尝试连接到使用DB Browser for SQLCipher 创建的空的、加密的 SQLite 数据库。 After creating the connection, I'm attempting to migrate the database, but I'm getting a
'SQLite Error 26: 'file is not a database'.'
创建连接后,我正在尝试迁移数据库,但我收到
'SQLite Error 26: 'file is not a database'.'
Here is my code:这是我的代码:
var connection = new SqliteConnection(@"Data Source=Sqlite\test.db;");
connection.Open();
var command = connection.CreateCommand();
var password = "test";
command.CommandText = "SELECT quote($password);";
command.Parameters.AddWithValue("$password", password);
var quotedPassword = (string)command.ExecuteScalar();
command.CommandText = "PRAGMA key = " + quotedPassword;
command.Parameters.Clear();
var result = command.ExecuteNonQuery();
services.AddDbContext<SmartContext>(options =>
options.UseSqlite(connection));
var serviceProvider = services.BuildServiceProvider();
var context = serviceProvider.GetService<MyContext>();
Console.WriteLine("Migrating sqlite database");
context.Database.Migrate();
I'm able to connect to the database in DB Browser and SQLiteStudio .我能够连接到DB Browser和SQLiteStudio中的数据库。
I've looked at this repository and I have the SQLitePCLRaw.bundle_sqlcipher (1.1.11) , SQLitePCLRaw.bundle_green (1.1.11) , Microsoft.EntityFrameworkCore.Design (2.2.6) , Microsoft.EntityFrameworkCore.Sqlite.Core (2.2.6) packages added in my project.我查看了 这个存储库,我有SQLitePCLRaw.bundle_sqlcipher (1.1.11) 、 SQLitePCLRaw.bundle_green (1.1.11) 、 Microsoft.EntityFrameworkCore.Design (2.2.6) 、 Microsoft.EntityFrameworkCore.Sqlite.Core (2.2. 6)在我的项目中添加的包。
I've looked at a lot of posts on a lot of forums, but none of them mention migrations with encrypted databases.我看过很多论坛上的很多帖子,但没有一个提到使用加密数据库进行迁移。 I might be missing something really obvious, but any help getting past this error would be greatly appreciated, thanks!
我可能遗漏了一些非常明显的东西,但是任何帮助解决这个错误将不胜感激,谢谢!
I think you need to add the Password XD我认为您需要添加密码 XD
Please check the Specify the key section in the official documentation if you have other problem.如果您有其他问题,请查看官方文档中的指定密钥部分。
The method for encrypting and decrypting existing databases varies depending on which solution you're using.
加密和解密现有数据库的方法因您使用的解决方案而异。 For example, you need to use the sqlcipher_export() function on SQLCipher.
例如,您需要在 SQLCipher 上使用 sqlcipher_export() function。 Check your solution's documentation for details.
查看解决方案的文档以了解详细信息。
You can use the SQLCipher API to finish your work:您可以使用 SQLCipher API 来完成您的工作:
If you want to build a encrypting database, just set your connection string, like this: Data Source = encryptedName.db; Password=YourPassword
如果要构建加密数据库,只需设置连接字符串,如下所示:
Data Source = encryptedName.db; Password=YourPassword
Data Source = encryptedName.db; Password=YourPassword
if you want to change password in a encrypting database如果要更改加密数据库中的密码
// you can use this in startup.cs using var changePasswordDb = new DBContext( new SqliteConnection( new SqliteConnectionStringBuilder() { DataSource = "encryptedName.db", Mode = SqliteOpenMode.ReadWriteCreate, Password = oldPassword }.ToString() )); changePasswordDb.Database.ExecuteSqlRaw($"PRAGMA rekey = {newPassword}"); // or use SqliteConnection var connection = new SqliteConnection("Data Source =encryptedName.db.db;Password=yourPassword"); connection.Open(); var command = connection.CreateCommand(); command.CommandText = "PRAGMA rekey = " + newPassword; command.ExecuteNonQuery();
if you want to encrypt a plaintext SQLite database, SQLCipher is not support directly.如果要加密明文 SQLite 数据库,则不直接支持 SQLCipher。 so you can use sqlcipher_export() to get the goal.
所以你可以使用 sqlcipher_export() 来达到目标。 look this How to encrypt a plaintext SQLite database to use SQLCipher (and avoid “file is encrypted or is not a database” errors)
看这个如何加密明文 SQLite 数据库以使用 SQLCipher(并避免“文件已加密或不是数据库”错误)
// Example
command.CommandText = "ATTACH DATABASE 'encryptedName.db' AS encrypted KEY 'YourNewPassword';SELECT sqlcipher_export('encrypted');DETACH DATABASE encryptedName;";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.