[英]How to implement Development and ProductionDatabase in ASP.NET MVC with Ado.NET Entity?
I have used Ruby on Rails with ActiveRecord, so I am quite used to switching to Production/Development database. 我已经在ActiveRecord中使用了Ruby on Rails,所以我已经习惯了切换到生产/开发数据库。
I am wondering, how do people implement Development and Production Database difference in ASP.NET MVC (preferably with ado.net entity). 我想知道,人们如何在ASP.NET MVC中(最好使用ado.net实体)实现开发和生产数据库的差异。
I tried to have it by creating 2 entity datasets with the same name in a different namespace, so I would only need to switch the used namespace in my database code. 我试图通过在不同的名称空间中创建两个具有相同名称的实体数据集来实现此目的,因此只需要在数据库代码中切换使用的名称空间即可。 Unfortunately, this didn't work because the entity datasets do not allow equal names...
不幸的是,这没有用,因为实体数据集不允许使用相同的名称...
The way I do it (with a web app) is to have separate versions of the Web.config file for each of Development (web.config), QA (web-qa.config), and Production (web-prod.config) installs. 我这样做的方法(使用Web应用程序)是为开发(web.config),质量检查(web-qa.config)和生产(web-prod.config)的每一个都有单独的Web.config文件版本。安装。 The config files have different connection strings pointing to the corresponding instances of the database.
配置文件具有指向数据库相应实例的不同连接字符串。 The default connection string points to the SQL server instance on my local machine which isn't available to the network.
默认连接字符串指向网络上不可用的本地计算机上的SQL Server实例。
As part of the publishing step, I manually delete the web.config file and rename the appropriate QA/Prod configuration to web.config. 作为发布步骤的一部分,我手动删除了web.config文件,并将适当的QA / Prod配置重命名为web.config。 This hooks up that instance of the app to the correct instance of the database.
这会将应用程序的该实例连接到正确的数据库实例。
最简单的方法(尽管它仍然不如Rails那样简单)是将连接字符串包含到App.config中 。
You should have one entity dataset, and instatiate the entities object using the constructor that takes a ConnectionString, which you can read in from: 您应该有一个实体数据集,并使用带有ConnectionString的构造函数来实例化实体对象,可以从中读取以下内容:
WebConfigurationManager.ConnectionStrings["ConnStrName"].ConnectionString;
The ConnectionStrings element also supports the general configSource attribute. ConnectionStrings元素还支持常规的configSource属性。
You can therefore have something like: 因此,您可以像以下内容:
<connectionStrings configSource="PathToConnectionStrings.config" />
This allows you to have all the connection strings held in a seperate file, that can then have different values on each environment. 这使您可以将所有连接字符串保存在单独的文件中,然后在每个环境中可以具有不同的值。
Note that unlike the appSettings file attribute, this doesn't do a merge, it completely replaces the section. 请注意,与appSettings文件属性不同,它不会进行合并,而是会完全替换该部分。
Note that in VS2010, you will have the facility to modify the .config based on the build configuration, so could change the values just by setting your build to "Production" say. 请注意,在VS2010中,您将具有根据构建配置来修改.config的功能,因此仅通过将构建设置为“生产”即可更改值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.