SQLitePCL.raw: SQLite Error 26: 'file is not a database' when using EF Core 3.1.2 and SQLitePCLRaw.bundle_e_sqlcipher
I feel sure it is me doing something wrong but I have been trying this for hours and no matter what I try I keep getting the exception below about the file is not a database. I have quadruple checked the location and password and they are correct and my older EF Core 2.1.X code can open the file and read from it just. It is the EF Core 3.1.2 project that I cannot get working. I also tried calling Batteries_V2 like the older code but that did not help.
Any suggestions?
I am using two nugets in my .NET Standard 2.0 project with EF Core 3.1.2 to created my entities assembly.
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="3.1.2" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlcipher" Version="2.0.2" />
</ItemGroup>
The Entities DbContext Code:
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
SQLitePCL.Batteries.Init();
}
public DbSet<Form> Forms { get; set; }
}
The code that uses the Entities is a .NET Core 3.1 MSTest app.
var connectionStringBuilder = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder
{
DataSource = @"Data Source=C:\Temp\MYDbLite.db",
Password = "KDSLKJLSDFJLSKDFKLDSKLFJSDFJKLSDFJKLSDF"
};
var dcob = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connectionStringBuilder.ConnectionString);
var db = new MyDbContext(dcob.Options);
var forms = await db.Forms.ToListAsync();
Message I get:
Test method HealthWare.Entities.POCLite.Tests.UnitTests.InstantiateTest threw exception:
Microsoft.Data.Sqlite.SqliteException: SQLite Error 26: 'file is not a database'.
Stack Trace:
SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext()
SqliteCommand.GetStatements(Stopwatch timer)+MoveNext()
SqliteDataReader.NextResult()
SqliteCommand.ExecuteReader(CommandBehavior behavior)
SqliteCommand.ExecuteReader()
SqliteCommand.ExecuteNonQuery()
SqliteConnectionExtensions.ExecuteNonQuery(SqliteConnection connection, String commandText, SqliteParameter[] parameters)
SqliteConnection.Open()
DbConnection.OpenAsync(CancellationToken cancellationToken)
— End of stack trace from previous location where exception was thrown —
RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)
RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)
RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
AsyncEnumerator.MoveNextAsync()
EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable1 source, CancellationToken cancellationToken) EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable
1 source, CancellationToken cancellationToken)
UnitTests.InstantiateTest() line 25
ThreadOperations.ExecuteWithAbortSafety(Action action)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 24 (7 by maintainers)
I know this is an older post, but I just started using SQLCipher and happened to come across this error while performing a “bad password” test. I created the database using a password and then opened with the same password. I then intentionally used a bad password to verify it would fail and it did. However, it failed with the SQLite Error 26: ‘file is not a database’ error. Just thought I’d pass this on in case someone in the future is searching for something similar.
The problem here is that you are trying to open a SQLCipher 3 database (the version used in the 1.x bundle) using SQLCipher 4 (the version used in the 2.x bundle) without properly migrating or setting backwards compatibility. Try either option on the upgrading page to resolve the issue.
@sjlombardo that appears to be the answer here. I was mistakenly under the impression that it encrypted the data once the password was set, at least on an empty database with only schema, but the attach and export article makes that super clear, it must be brand new in order to encrypt it with SQLCipher. I have not done a lot more testing yet but in a quick test I was able to attach my example unencrypted database, export it to a new encrypted database per the instructions in the article above, and then open the encrypted database using Microsoft.Data.Sqlite with the password supplied in its connection string. AWESOME.
@bricelam maybe you can update the article on your website to make it clear that you cannot encrypt an existing unencrypted database by only supplying the password in the connection string and point them to the attach and export article if they are trying to encrypt an existing database.
My sincere thank you to everyone that responded here and for the fact that the responses were so quick!
So taking from this https://www.bricelam.net/2016/06/13/sqlite-encryption.html, I wrote a quick .NET Core 3.1 Console app and created a new unencrypted SQLite database with one table and tried to encrypt it by passing in the password in the connection string as shown above. When I do this I get
Microsoft.Data.Sqlite.SqliteException: ‘SQLite Error 26: ‘file is not a database’.’
If I remove the password from the connection string it opens the database and executes the query just fine.
So maybe I am just totally misunderstanding but I thought based on what I read above that opening an unencrypted database with a password would then key that database to the password supplied in the connection string and then that password would be required from then on.
I attached the example console app.
Do I need to call something other than SQLitePCL.Batteries.Init() or call it in a different place? Should I use the Batteries_V2? I tried a number of combinations, but they all had the same results but I definitely could be doing it wrong.
Hope the sample helps and thank you so much for looking at this.
SQLiteEncryption.zip