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?

2
  • 1
    According to this 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. Commented Apr 15, 2018 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' Commented Apr 15, 2018 at 15:59

1 Answer 1

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"); 
   }
Sign up to request clarification or add additional context in comments.

11 Comments

How does changing the password resolve the connection string issue? Also you code has errors.
ChangePassword method changes password if there's an existing password, if there isn't,it sets the password :)
Yeah but how will that help Entity Framework connect?
My answer answer only How to set password for sqlite , isn't that what OP is asking for ?
That's not the way I read it. OP says "I know how to protect a SQlite database..."
|

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.