I have an ASP.NET Core console application (targeting version 2.2), using Entity Framework Core. I'm trying to connect to an empty, encrypted SQLite database created with DB Browser for SQLCipher. After creating the connection, I'm attempting to migrate the database, but I'm getting a '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.
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.
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!