문제

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

도움이 되었습니까?

다른 팁

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top