简体   繁体   English

如何首先为生产环境配置EF数据库?

[英]How to configure EF database first for production environment?

So we have developed an MVC application with EF database first and are trying to deploy the solution to Production environment. 因此,我们首先开发了具有EF数据库的MVC应用程序,并正在尝试将该解决方案部署到生产环境中。 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? 首先部署EF DB的方法是什么?

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 它将用新值重写您的web.config

When deploying to a production environment, an application is typically compiled in Release mode. 部署到生产环境时,通常在Release模式下编译应用程序。 In this mode you can use Web.Release.config to specify the variables that should be different in production. 在这种模式下,您可以使用Web.Release.config来指定在生产中应该不同的变量。

Say your connection string in Web.config is 假设您在Web.config的连接字符串是

<add name="MyContext"
     connectionString="DevConnectionString" 
     providerName="System.Data.SqlClient" />

You can add this in Web.Release.config : 您可以在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". 部署到生产环境时,连接字符串将自动更改为“ ProdConnectionString”。

For more information on Web.config transforms see How to: Transform Web.config When Deploying a Web Application Project 有关Web.config转换的更多信息,请参见如何:在部署Web应用程序项目时转换Web.config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM