1

Using: Visual Studio Community 2015 + C# + Entity Framework 6.2 + SQlite2

I know how to protect a SQlite database as I use the below code to attach password to it :

using (sqlite2 = new SQLiteConnection("Data Source=ModuleUserDB.db"))
{
    //Set password
    sqlite2.SetPassword("this is my password");
}

But this doesn't work with Entity Framework.

I read this post How to in-code supply the password to a connection string in an ADO.Net Entity Data Model

but I can't figure out the solution for my case

  • DbContext name: ModuleUserDBEntities
  • Database name: ModuleUserDB.db

Trying to modify the constructor with:

public partial class ModuleUserDBEntities : DbContext
{
    public ModuleUserDBEntities() : base("name=ModuleUserDBEntities")
    {
        // Set the password of the database
        this.Database.Connection.ConnectionString = @"Data Source=.\;Initial Catalog=ModuleUserDB.db;Persist Security Info=True;User ID=sa;Password=myownpassword";
    }
}

But Visual Studio gets stuck.

What is the simplest connection string to use for this case?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
fedeteka
  • 943
  • 1
  • 14
  • 33
  • 1
    According to [this](https://www.connectionstrings.com/sqlite/) there is no username in the connection string. And please use a better problem description than "Visual Studio gets stucks". If there is an exception provide the entire exception detail. – Crowcoder Apr 15 '18 at 15:46
  • @Crowcoder Just Visual Studio gets stucks with this info on the Inmediate Window: Step into: Stepping over non-user code 'WpfApplication1.App..ctor' Step into: Stepping over non-user code 'WpfApplication1.App.InitializeComponent' – fedeteka Apr 15 '18 at 15:59

1 Answers1

2

Give ChangePassword method a try.Sample :

   using (sqlite2 = new SQLiteConnection("Data Source=ModuleUserDB.db"))
   {
    sqlite2.Open();  //You must open the connection first
    sqlite2.ChangePassword("password here"); 
   }
Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • 1
    How does changing the password resolve the connection string issue? Also you code has errors. – Crowcoder Apr 15 '18 at 15:47
  • 2
    `ChangePassword` method changes password if there's an existing password, if there isn't,it sets the password :) – Software Dev Apr 15 '18 at 15:48
  • 1
    Yeah but how will that help Entity Framework connect? – Crowcoder Apr 15 '18 at 15:49
  • 1
    My answer answer only `How to set password for sqlite` , isn't that what OP is asking for ? – Software Dev Apr 15 '18 at 15:51
  • 1
    That's not the way I read it. OP says "I know how to protect a SQlite database..." – Crowcoder Apr 15 '18 at 15:52
  • 1
    yes, but he also said that his code doesn't work with entityframework....I am not sure if mine will work with entityframework either, let's wait for the OP's response :) – Software Dev Apr 15 '18 at 15:54
  • @zackraiyan thanks for the support, with youre code I get "System.Data.SQLite.SQLiteExcepcio "File is not a database not an error" Error code 26" – fedeteka Apr 15 '18 at 16:12
  • 1
    Is that the error ? It makes no sense..recheck the error..I assume it should be `File is not a database or is encrypted` or something like that – Software Dev Apr 15 '18 at 16:15
  • 1
    I hope my code helps you and if it does,mark it as the answer..And about the error you are getting,[this](https://stackoverflow.com/questions/44161713/sqlite-file-is-encrypted-or-is-not-a-database) will solve it – Software Dev Apr 15 '18 at 16:17
  • @zackraiyan the error is the same, "File is not a database not an error" Error code 26" and I opened the SQLite database using a portable viewer and the database never gets encrypted. I will keep trying, thanks for the support. – fedeteka Apr 15 '18 at 16:24
  • @zackraiyan Your code work but for a database outside the Model - Entity Framework. Once I protect the data base with the code the others parts of the program (list records by example) didn´t work anymore. Thanks anyway, I read a lot of tutorials on these days and it seems complicated to use SQlite with password protection on Entity Framework – fedeteka Apr 18 '18 at 12:06