简体   繁体   English

如何使用 Entity Framework Core 在加密的 SQLite 数据库上运行迁移?

[英]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 BrowserSQLiteStudio中的数据库。

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 来完成您的工作:

  1. 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

  2. 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();
  3. 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;";
  1. if you have some Performance Issue in Asp.net Core and Entity Framework Core, you need to check to these如果您在 Asp.net 核心和实体框架核心中有一些性能问题,您需要检查这些
  1. if you have other Performance Issue, you can check this SQLCipher Performance Optimization如果你有其他性能问题,你可以检查这个SQLCipher Performance Optimization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用实体框架在 .NET Core 3.1 中进行迁移后,如何更新数据库 model? - How can I update the database model after doing a migration in .NET Core 3.1 using Entity Framework? 如何使用Entity Framework 4.2执行数据库迁移? - How can I perform database migration using Entity Framework 4.2? 如何使用 Entity Framework Core 运行迁移 SQL 脚本 - How to run migration SQL script using Entity Framework Core C# 中的 Entity Framework Core 数据库迁移 - Entity Framework Core database migration in C# 实体框架核心 - Database.Migration() - Entity Framework Core - Database.Migration() 在Entity Framework数据库迁移期间应如何运行自定义代码? - How should custom code be run during an Entity Framework database migration? 如何忽略 Entity Framework Core SQLite 数据库中的外键约束? - How to ignore Foreign Key Constraints in Entity Framework Core SQLite database? 实体框架和加密数据库 - Entity Framework and Encrypted Database 我可以迁移到在Entity框架中无迁移创建的数据库吗? - Can I migrate to database created without migration in Entity framework? 如何使用Sqlite设置Entity Framework Core 2.1? -将数据库连接到我的解决方案 - How do I setup Entity Framework Core 2.1 with Sqlite? - Connecting Database to My Solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM