So we have developed an MVC application with EF database first and are trying to deploy the solution to Production environment. however the database server is on a different server and hence we would have to provide a conenction string pointing to the DB server. We tried couple of methods but weren't successful.
Whats the approach for deployments for EF DB first?
Have you tried this code?
Configuration connectionConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
connectionConfiguration.ConnectionStrings.ConnectionStrings["test"].ConnectionString = txtConnectionString.Text;
connectionConfiguration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
It will rewrite your web.config with new values
When deploying to a production environment, an application is typically compiled in Release
mode. In this mode you can use Web.Release.config
to specify the variables that should be different in production.
Say your connection string in Web.config
is
<add name="MyContext"
connectionString="DevConnectionString"
providerName="System.Data.SqlClient" />
You can add this in Web.Release.config
:
<add name="MyContext"
connectionString="ProdConnectionString"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)" />
When deploying to production the connection string will automatically change to "ProdConnectionString".
For more information on Web.config transforms see How to: Transform Web.config When Deploying a Web Application Project
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.