1

Can anyone suggest how I could programatically switch debug and live connection strings?

I've seen other people have passed an EntityConnection to the constructor from the controller like this :

private XYZDatabase db = new XYZDatabase(ConfigurationManager.
ConnectionStrings["XYZDatabase-TEST"].ConnectionString);

but it still requires manually changing it? is there a way to use

System.Net.Dns.GetHostName() or similar to switch it automatically?

Thanks

Mike
  • 369
  • 1
  • 6
  • 24

3 Answers3

1

Ayende has a post that addresses this issue.

http://ayende.com/blog/135169/frictionless-development-web-config-and-connection-strings

ckal
  • 3,540
  • 1
  • 22
  • 21
0

In my software I just have 2 connection strings of the same name in my app.config and comment/uncomment to switch between configurations.

Another method is that you could create a static property in your solution (.asax file?) and just use that to swap between XYZDatabase-TEST and XYZDatabase when you are fetching ConnectionStrings.

Malcolm O'Hare
  • 4,879
  • 3
  • 33
  • 53
0

Add connection string to your Web.config like:

<connectionStrings>
    <add name="XYZDatabase-TEST" connectionString="Server=.\SQLEXPRESS;Database=XYZDatabase-TEST";integrated security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

Then open you Web.Release.config and add

<connectionStrings>
    <add name="XYZDatabase-TEST"
        connectionString="Data Source=OTHERSERVER;Initial Catalog=XYZDatabase-TEST;Persist Security Info=True;User ID=sa;Password=password" providerName="System.Data.SqlClient"
        xdt:Transform="SetAttributes"
        xdt:Locator="Match(name)"/>
</connectionStrings>

Now, everytime you will publish you application to deployment server using Release configuration it will use the connection string from web.release.config

Note that this transformation will not work locally when you debug. You must publish to run the web.config transformation.