开发和生产数据库 [英] Development and Production databases

查看:107
本文介绍了开发和生产数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的团队当前正在做一个项目,我们正在使用Entity Framework 4.1(代码优先).我们想编写一些测试,但是我们不希望它们在我们的主数据库上运行,因为我们在新加坡有一个团队为我们所做的事情编写客户端,并且他们一直在访问该数据库.

I and my team are currently doing a project, where we are using Entity Framework 4.1 (Code First). We want to write some tests, but we don't want them to run on our primary database, as we have a team in Singapore writing a client for what we do, and they are hitting that database constantly.

因此,为了避免在运行测试时造成干扰,我们希望使用其他数据库进行测试.使用实体框架时,我们如何处理第二个数据库?我们需要一个(至少)半自动的解决方案,因此我们不必在每次需要运行测试时都弄弄Web.config.

So to avoid disturbance when running our tests, we would like to have a different database for testing. How do we handle a second database when using Entity Framework? We want a solution that is semi-automatic (at least), so we don't have to fiddle around with Web.config each time we need to run tests.

推荐答案

弄乱web.config的过程可能容易出错……除非您使用的是

Fiddling around with the web.config can a process that is prone to error... unless you are using web.config Transformations that is.

我将在Visual Studio中为您的项目创建一个新配置测试",它可以是现有开发配置的副本(或调试/发行版,无论如何).然后,在解决方案资源管理器中右键单击您的Web.config文件,然后单击添加配置转换.按照此处的说明操作,以了解如何编写转换文件.如果您只需要更改测试环境的EF连接字符串,它将在web.Test.config中看起来像这样:

I would create a new configuration, "Test" for your project in Visual Studio... it can be a copy of your existing development configuration (or Debug / Release, whatever). Then, right click your Web.config file in Solution Explorer and click Add Config Transforms. Follow the instructions here on how to write a transform file. If you only need to change the EF connection string for the test environment it would look something like this in web.Test.config:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
    <add name="AdventureWorksEntities" 
     connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
     provider=System.Data.SqlClient;provider connection string='Data Source=TestDB;
     Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;
     multipleactiveresultsets=true'" providerName="System.Data.EntityClient" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

要运行测试,只需确保在正确的配置下进行构建.

Just be sure to build under the correct configuration when you want to run your tests.

还有一个Visual Studio加载项 SlowCheetah 这使得整个过程在IDE中非常无缝.

There is also a Visual Studio Add-in SlowCheetah Which makes this whole process very seamless from within the IDE.

这篇关于开发和生产数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆