0

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=&quot;Data Source=**Server1**\**DATABASE1**;Persist Security Info=True;User ID=<STANDARDUSER>;Password=<STANDARDPASSWORD>Qu3r3y3;MultipleActiveResultSets=True;Application Name=EntityFramework&quot;" 
         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=&quot;Data Source=**Server2**\**DATABASE2**;Persist Security Info=True;User ID=<STANDARDUSER>;Password=<STANDARDPASSWORD>Qu3r3y3;MultipleActiveResultSets=True;Application Name=EntityFramework&quot;" 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);
Community
  • 1
  • 1
Ian W
  • 385
  • 2
  • 10
  • 30
  • does the app for the same companies share the same functionality? if so then add 2 different connection strings.. and a variable that can differentiate between the 2 companies there are several ways to approach this without making it so difficult – MethodMan Jan 14 '16 at 21:58
  • Why don't you just have two `app.config` - `app.company1.config` and `app.company2.config` and just pick the correct one at the time of installation and copy that to the customer's server – marc_s Jan 14 '16 at 22:01
  • 2
    2 different app.configs would be best, overwriting data doesn't seem right. Exposing password from one company to another company alone should be reason enough not to do this... – Alexander Derck Jan 14 '16 at 22:02
  • @MethodMan yes they share the same functionality as they are the same group of companies and the app links to the Group ERP SQL database. The problem with your approach is how do I tell the Entity Model which of the two connection strings to use or do I create a conditional dbContext base on my variable – Ian W Jan 14 '16 at 22:54
  • @AlexanderDerck Yes I can have two app.config but I wanted to give the users the ability to pick as they all work for the same Group of companies and there are times when a user in CompanyA will do the same job in CompanyB due to illness etc – Ian W Jan 14 '16 at 22:56
  • @marc_s see the above comments, but yes I could have two app.config – Ian W Jan 14 '16 at 22:57
  • How are you deploying? If using msdeploy, look into parameters.xml. – Jeff Dunlop Jan 15 '16 at 03:22

0 Answers0