发布时将连接字符串从开发更改为生产 [英] Change connection string from development to production when publishing
问题描述
我想知道如何自动更改应用程序的连接字符串,因此当我在PC上使用它时,它将使用本地SQL Server,并且一旦发布,便会使用我在azure上托管的SQL Server
I want to know how to change automatically the connection string of my app so when I'm working on it in my pc, it uses my local SQL Server and once I publish it then uses the SQL Server I have hosted on azure.
现在,我只是根据我想要的内容注释掉连接字符串,我看到Web.Config文件具有2个依赖项,即Web.Debug.config和Web.Release.config,这是我想拥有的依赖项做某事,但我不知道该怎么做.
Right now I'm just commenting out the connection string depending of what I want, I saw that the Web.Config file has 2 dependencies, Web.Debug.config and Web.Release.config which is where I guess I have to do something but I don't know what.
到目前为止,这是我的web.config文件
This is my web.config file so far
<connectionStrings>
<add name="MyApp" connectionString="Data Source=mydb.database.windows.net;Initial Catalog=MyDb;User ID=MyUser@mydb.database.windows.net;Password=MyPwd;Encrypt=true;Trusted_Connection=false;" providerName="System.Data.SqlClient" />
<!--<add name="MyApp" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=MyDb;Integrated Security=True;" />-->
</connectionStrings
如您所见,我有2个连接字符串已注释掉,但这确实很烦人.
As you can see I have 2 connection strings that I have comment out but doing this is really annoying.
推荐答案
这两个配置是针对各自的构建设置构建的,因此您可以将dev连接字符串放在Web.Debug.config
中,而prod连接字符串放在Web.Release.config
中.然后,当您部署到生产环境时,运行一个Release版本,然后您得到的Web.Config将具有生产环境连接字符串.
The two configs are built for their respective build setting, so you can put your dev connection string in Web.Debug.config
and your prod connection string in Web.Release.config
. Then, when you deploy to production, run a Release build and your resulting Web.Config will have the production connection string.
这篇关于发布时将连接字符串从开发更改为生产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!