title: App.config connectionString change at startup
body: <p>I have a very short question that is partially answered but doesn't seem to work.</p>
  
  <p>I have my SQL Server with its database.</p>
  
  <p>I want to change the app.config's connectionString's data source at startup so that it was valid with my computer's name.</p>
  
  <p>This is the part of my app.config that I want to change (replace the
      whole connectionString):</p>
  
  <pre><code>&lt;connectionStrings&gt;
        &lt;add name="MovieDatabaseEntities" connectionString="metadata=res://*/MovieDatabase.csdl|res://*/MovieDatabase.ssdl|res://*/MovieDatabase.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=VAIO;initial catalog=MovieDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;"providerName="System.Data.EntityClient" /&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>This is my code that should do the thing:</p>
  
  <pre><code>string HostName = System.Environment.MachineName;
  System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  config.ConnectionStrings.ConnectionStrings["MovieDatabaseEntities"].ConnectionString = ("metadata=res://*/MovieDatabase.csdl|res://*/MovieDatabase.ssdl|res://*/MovieDatabase.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=" + HostName + ";initial catalog=MovieDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;");
  config.Save(ConfigurationSaveMode.Modified, true);
  ConfigurationManager.RefreshSection("connectionStrings");
  </code></pre>
  
  <p>Code runs but doesn't seem to do anything. App.config remains unchanged.
  Why?</p>