I have the following problem:
EF creates a connectionString in the app.config of the application and always uses that connection, but in case you need to change the server of the bank I will have to change the app.config.
I have tried a way to make the server ip exchange work, but when I do the same it takes & of the & quote symbol and looks like this: & quote; then when I start the application it automatically gives error because it does not recognize the information. Here is the code for the app.config and code I made via C # to change that 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 pfv. 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 run the application and take the settings file Menu.exe.config (In case the exe name is Menu) it does not change this file and I want to be able to change this file, is it possible?
Update 3 - On the site link found an example that uses the xml change in the .config file, so I could also realize it uses a StringBuilder, the only part of the code I did not understand was: xElement.FirstChild.Attributes[2].Value = con;
, can someone help me understand? I'm almost coming to a solution I just need some help.