I have a desktop app that I am nearing finishing proof of concept on but I have a final function I want to add but I do not think it is possible
The app which will finally be deployed uses Entity Framework and is written in VS 2010. The app needs to be deployed in two different companies which have the same SQL database structure (but different names Database1
or Database2
) but they are hosted on different servers (eg Server1
and Server2
)
It would be neat to be able to set Server1
or Server2
and Database1
or Database2
in a wpf form and "save" the application setting rather than have to deploy twice with different EF models.
This means writing over the app.config
file at run time and from reading around this seem difficult see here but this refers to general settings not the configuration string or am I missing the point
In my case should my app.config
is
<connectionStrings>
<add name="OsmiContext"
connectionString="metadata=res://*/OsmiModel.csdl|res://*/OsmiModel.ssdl|res://*/OsmiModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=**Server1**\**DATABASE1**;Persist Security Info=True;User ID=<STANDARDUSER>;Password=<STANDARDPASSWORD>Qu3r3y3;MultipleActiveResultSets=True;Application Name=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
So I think my code should look something like this I think (the String NewConnection
would in fact be built in the method at run time) Am I right or missing something?
<build so new App.Config Method>
String NewConnection =@"metadata=res://*/OsmiModel.csdl|res://*/OsmiModel.ssdl|res://*/OsmiModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=**Server2**\**DATABASE2**;Persist Security Info=True;User ID=<STANDARDUSER>;Password=<STANDARDPASSWORD>Qu3r3y3;MultipleActiveResultSets=True;Application Name=EntityFramework"" providerName="System.Data.EntityClient"
string appPath =System.IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location);
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["OsmiContext"].Value = NewConnection ;
config.Save(ConfigurationSaveMode.Modified);