3
I have the following problem:
EF creates a connectionString in the app.config and uses this connection always, but if you need to change the database server I will have to change the app.config.
I have tried a way to make the server ip exchange work, but when I run it it takes the & of the "e symbol and is as follows: "e; ai when I start the application automatically gives error because it does not recognize the proper information. Follow the code of the app.config and code I made via C# to change this property.
App.config
<connectionStrings>
<add name="ref_bdEntities" connectionString="metadata=res://*/ModeloDados.csdl|res://*/ModeloDados.ssdl|res://*/ModeloDados.msl;provider=MySql.Data.MySqlClient;provider connection string="server=127.0.0.1;user id=sa;password=master;persistsecurityinfo=True;database=ref_bd"" providerName="System.Data.EntityClient" />
</connectionStrings>
Change code
string _ipservidor = string.Empty;
_ipservidor = LerIni.LerINI(@"C:\Motion Software\bin\config.ini", "[IPServidor]");
connectionString = "metadata=res://*/ModeloDados.csdl|res://*/ModeloDados.ssdl|res://*/ModeloDados.msl;provider=MySql.Data.MySqlClient;provider connection string="server="+_ipservidor+";user id=sa;password=master;persistsecurityinfo=True;database=ref_bd"";
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["ref_bdEntities"].ConnectionString = connectionString;
config.Save(ConfigurationSaveMode.Modified); // Salva o que foi modificado
ConfigurationManager.RefreshSection("connectionStrings"); // Atualiza no app o bloco connectionStrings
Properties.Settings.Default.Reload(); // Recarrega os dados de conexão
Help me with this. I am using Mysql as a database and will probably use a hosting for the database (ignore the fact that it is Windows Forms).
Update - I’m using Database First
Update 2 - The above code does not change the value of the physical config file, when I put the necessary files to work the application and take the settings file Menu.exe.config (In the case the exe name is Menu) it does not alter that file and I want to be able to change that file, it is possible?
Update 3 - No website http://www.codeproject.com/Tips/411013/Configuring-a-Connection-String-in-the-App-Config I found an example that uses the xml change in the file . config, from what I could also perceive he uses a Stringbuilder, the only part of the code I didn’t understand was: xElement.FirstChild.Attributes[2].Value = con;
, can anyone help me understand? I’m almost to a solution just need some help.