0

Gurus.

I'm try add a password string in a DbContext constructor, but I can't.

How do I get an actual connection strings values from app.config file and add a password string in DbContext constructor C#?

I'm using EF 6 and winforms C#.

Thanks in advance.

public partial class ficharioEntities : DbContext
{
    public ficharioEntities()
        : base("name=ficharioEntities")
    {

        // How get a actual connection strings values from app.config file and add a password string here

    }

UPDATE:

I have just used this post How to in-code supply the password to a connection string in an ADO.Net Entity Data Model to solve my own question, with a little change in code.

    public ficharioEntities()
        : base("name=ficharioEntities")
    {

        var originalConnectionString = ConfigurationManager.ConnectionStrings["ficharioEntities"].ConnectionString;
        var entityBuilder = new EntityConnectionStringBuilder(originalConnectionString);
        var factory = DbProviderFactories.GetFactory(entityBuilder.Provider);
        var providerBuilder = factory.CreateConnectionStringBuilder();
        providerBuilder.ConnectionString = entityBuilder.ProviderConnectionString;
        providerBuilder.Add("Password", "<password_here>");

        this.Database.Connection.ConnectionString = providerBuilder.ToString();
    }

With this part of code "this.Database.Connection.ConnectionString = providerBuilder.ToString();" I can use my constructor without any parameters (as I wanted).

Community
  • 1
  • 1
Cesco
  • 1
  • 1
  • 2
    Possible duplicate of [How to in-code supply the password to a connection string in an ADO.Net Entity Data Model](http://stackoverflow.com/questions/8170118/how-to-in-code-supply-the-password-to-a-connection-string-in-an-ado-net-entity-d) – blins Jun 29 '16 at 02:00
  • Or here http://stackoverflow.com/a/20254520/1862333 – failedprogramming Jun 29 '16 at 02:01
  • Thanks, @blins... I've just used this post "How to in-code supply the password to a connection string in an ADO.Net Entity Data Model" **with a little code change** to solve my own question. I update my question with a solution (I dont know if it's right to answer this way). – Cesco Jun 29 '16 at 03:19

2 Answers2

0

You should be able to add a private function that changes the entity framework connection string password.

public partial class ficharioEntities : DbContext
{
     public ficharioEntities(string password)
                        :base(GetEntityConnectionString(password))
     {


     }

     private static string GetEntityConnectionString(string password){
          string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["fichario"].ConnectionString;
          System.Data.EntityClient.EntityConnectionStringBuilder entityconnection = new System.Data.EntityClient.EntityConnectionStringBuilder(s);

       //EntityConnectionStringBuilder inherits from DBConnectionStringBuilder 
      //so you should be able to replace the password simply
      entityconnection["Password"] = password;

     //otherwise, if you know the connection properties you need
    //you could rebuild an entity connection object.

        return entityconnection.ToString();
     }
}
mfreedm52
  • 161
  • 10
0

Considering your problem I have a couple solutions you can use. this ties into basic plaintext file IO so make sure you check a couple tutorials on that later to get a bit more familiar.

1 : JSON

If your config file uses the JSON format you can use a JSON parser to get the information you need. Its easy to read for both you and the computer , making it a useful way to store information in plaintext files that can be edited with little pain. Look for C# JSON parsing if thats the case.

2 : StreamReader + String.Contains

If your config file has no format or uses its own format , but is still somewhat neatly formatted or easy to read , the next thing you can do is use a StreamReader to read from the file then use String.Contains to identify where the parameter you need is and finally extract that from the file. It sounds easier than it is because most of the time String.Contains wont be able to deal with the format / surrounding characters. In this case , you use...

3: StreamReader + REGEX

Same as before , except when you need to find information in complicated or messy plaintext files String.Contains wont be able to identify complex variating patterns within the text. See , while String.Contains can identify exact character patterns , REGEX can use special characters to deal with complex patterns. For example , the regex \s\w+\s finds any amount of letters inbetween two spaces. So if you need to find your information within complex patterns , regex is the way to go. I suggest you find an online regex tester and build the regex around the thing you need to find.

r3eckon
  • 14
  • 3
  • Thanks for your answer, @r3eckon... but I want use the config file default (app.config). – Cesco Jun 29 '16 at 03:23