I have ConnectionString defined in App.config file and I check the option not to include sensitive data so the password is left out from the connection string. In the application code I’m using Entity Framework and I’m accessing the db like this:
using (var db = new Entities())
{
List<DATA> d = db.DATA.ToList();
}
I want authenticate user by the DB password – user enters it when the application launches.
The question is, how to add this password to the connection string when I don’t handle the ConnectionString manually in the code?
The farther I got is this:
ConnectionStringSettings conn = ConfigurationManager.ConnectionStrings["Entities"];
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(conn.ConnectionString);
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(entityBuilder.ProviderConnectionString);
sqlBuilder.Password = "pwd";
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
ConnectionStringSettings newConn = new ConnectionStringSettings("Entities",entityBuilder.ConnectionString);
But this still does not work and I cannot assing the new connection string to the ConfigurationManager.ConnectionStrings – it says it is read only value. How to overcome this?
You can’t assign
ConfigurationManager.ConnectionStringsbecause that just reflects yourApp.config, which your app can’t change.But you can tell the EF which connection string to use: