简体   繁体   English

如何使用Ado.NET实体在ASP.NET MVC中实现Development and ProductionDatabase?

[英]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.

相关问题 如何将 ado.net 数据库连接到 ASP.Net MVC 5 - How to connect ado.net database to ASP.Net MVC 5 如何仅通过添加ADO.NET实体数据模型ASP.NET MVC 3连接到SQL Server数据库和获取数据 - How to Connect to SQL Server Database and Fetch datas by Just adding ADO.NET Entity Data model ASP.NET MVC 3 删除行ASP.NET MVC ADO.NET实体模型 - Delete row ASP.NET MVC ADO.NET entity Model ASP.NET MVC项目中已为ADO.NET实体模型打开的DataReader - An already open DataReader for the ADO.NET Entity model in ASP.NET MVC project 如何在 Asp.Net Core Mvc 5.0 中将 sql 数据库与 ado.net 连接? - How to connect sql Database with ado.net in Asp.Net Core Mvc 5.0? 如何在 ASP.NET Core MVC 中使用 ADO.NET 将参数添加到存储过程? - How can I add parameters to a stored procedure using ADO.NET in ASP.NET Core MVC? 如何使用通用存储库实现 ADO.NET? - How to implement ADO.NET with Generic Repository? 每个表行的ASP.NET MVC ADO.NET查询 - ASP.NET MVC ADO.NET Query per table row 将ADO.NET与ASP.NET MVC一起使用时的语法错误 - Syntax error while using ADO.NET with ASP.NET MVC ASP.NET MVC2:是否可以使用ado.net建立自己的模型 - ASP.NET MVC2 : Is it possible to make his own model, withou ado.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM