1

谁能建议我如何以编程方式切换调试和实时连接字符串?

我已经看到其他人从控制器将 EntityConnection 传递给构造函数,如下所示:

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

但它仍然需要手动更改它?有没有办法使用

System.Net.Dns.GetHostName()或类似的自动切换?

谢谢

4

3 回答 3

1

Ayende 有一篇文章解决了这个问题。

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

于 2012-12-10T18:53:52.997 回答
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.

于 2012-12-10T16:40:53.680 回答
0

将连接字符串添加到您的 Web.config,例如:

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

然后打开你的 Web.Release.config 并添加

<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>

现在,每次您使用发布配置将应用程序发布到部署服务器时,它将使用来自 web.release.config 的连接字符串

请注意,当您调试时,此转换不会在本地工作。您必须发布才能运行 web.config 转换。

于 2012-12-11T07:49:24.823 回答