12

I am trying to create a password protected SQLite database to use within a WPF application using Entity Framework Core.

I know how to generate DbContext and Entities from an existing database but don't know how to create a password protected database. What is the difference between an encrypted database and a password protected database?

3
  • 1
    you encrypt the DB via dbCon.SetPassword(myLitePW) thereafter you use the PW in the connection string: Data Source=c:\mydb.db;Version=3;Password=myLitePW; Commented Oct 6, 2016 at 19:10
  • 2
    Unless I am mistaken, this works with EF6 (within the System.Data.SQLite package). But with EF Core (within the Microsoft.Data.SQLite package), I can't find a way to do so ... (there is no SetPassword methods in the SQLiteConnection from the Microsoft.Data.SQLite) Commented Oct 6, 2016 at 20:22
  • "What is the difference between an encrypted database and a password protected database?" There is no password without encryption. Commented Nov 20, 2022 at 15:19

2 Answers 2

18

According to this article and this issue, there is no way (yet?) to encrypt the database using the Microsoft.Data.Sqlite assembly (used by EF Core).

Based on this, here is what I've done to get it working with EF Core:

  • add the System.Data.SQLite.Core package to the project
  • while configuring your dbContext, give the optionsBuilder your own DbConnection:

    var conn = new SQLiteConnection(@"Data Source=yourSQLite.db;");
    conn.Open();
    
    var command = conn.CreateCommand();
    command.CommandText = "PRAGMA key = password;";
    command.ExecuteNonQuery();
    
    optionsBuilder.UseSqlite(conn);
    

It is very important to use the SQLiteConnection (which can manage encrypted database) from the System.Data.SQLite.Core assembly and not the SqliteConnection from Microsoft.Data.Sqlite.


According to the article, you could probably used the built in SqliteConnection by replacing the sqlite3.dll shipped within the Microsoft.Data.Sqlite assembly by another one that handle encrypted database (you can find a free one on this repo). But I did not tested it.

Here is how to do this !

Sign up to request clarification or add additional context in comments.

Comments

1

I was pleasantly surprised with how smooth the solution described here was for me.

  1. Navigate to the github repo and scroll down to the steps.

  2. Install/remove the NuGet in steps 1 to 4.

  3. Modify your DbContext bit:

    public class MyDbContext: DbContext {
    
      public MyDbContext(): base() {}
    
      protected override void OnConfiguring(DbContextOptionsBuilder builder) {
          SqliteConnectionStringBuilder builder = new SqliteConnectionStringBuilder();
          builder.DataSource = @"myDatabase.db";
          builder.Password = "myPassword";
    
          optionsBuilder.UseSqlite(new SqliteConnection(builder.ConnectionString));
      }
    
      //... and some other unrelated stuff ...
    
    }
    

Some notes:

  • If you don't really care for the existing data, it might be easier to just delete the previous database. If you do care, you can give it a shot.
  • If you already have 'Microsoft.EntityFrameworkCore.Tools' installed, you don't need to install the 'Microsoft.EntityFrameworkCore.Design' NuGet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.