如何使用实体框架5在测试和生产数据库之间切换5 [英] How to switch between test and production databases with Entity Framework 5
问题描述
编辑:由于我是如何完成的应用程序被安排,我将需要使用GvM的答案。最后一件事情是,我不知道如何向基类发送一个参数。这是我的意思:
public class YourContext:DbContext {
public YourContext():base (yourNameOrConnectionString)
{
}
}
问题是这段代码不起作用:
string dbConnectionString =MyDBTest;
使用(var db = new YourContext(dbConnectionString))
{
//使用db
}的代码
如何向基类发送参数,在这种情况下是YourContext():base()
使用config转换。这就是他们在那里。诚然,尽管如此,配置转换有点混乱,因为调试器实际上并没有使用它们。让我详细说明。
默认情况下,您将获得三个web.configs: Web.config
, Web.Debug.config
和 Web.Release.config
。后两者与解决方案资源管理器中的第一个组合,但您可以扩展 Web.config
来查看它们。 Web.Debug.config 和 Web.Release.config
是转换。他们使用特殊的XML风格语法来允许您在主要 Web.config
文件中更改或转换设置。每个对应一个配置,即调试和释放,默认情况下这两个内置于Visual Studio中。您可以根据需要添加其他配置。
现在,这里是令人困惑的地方。 Debug
配置,尽管它的名字,在调试时从来没有被实际使用。更好的名字可能是开发
或分期
;这是当您在测试能力部署站点而不是生产时的配置。 发布
config用于生产。所以,您所需要的是您的主要的 Web.config
,请指定本地开发数据库的连接字符串。然后,在 Debug
和 Release
configs中,添加一个转换以将其更改为分段/生产数据库连接字符串, 分别。当您在本地调试时,将使用主要的 web.config
,然后在发布应用程序时,您将选择使用调试
或发布
,根据您将要部署的环境,然后转换将会运行更改发布的 web.config
。
有关转换及其编写方式的更多信息,请参阅: http://msdn.microsoft.com/en-us/library /dd465326(v=vs.110).aspx
I've got separate test and production databases for my ASP.NET MVC 4 application. Using Entity Framework 5, how can I have Entity Framework switch between these two databases programmatically? The application knows which database should be used at which time, so I just need for the application to be able to change Entity Framework so that it utilizes the correct database at the correct time.
Anyone know how to accomplish this and/or have a good example available?
EDIT: due to how my app is arranged, I am going to need to utlize GvM's answer. The last remaining thing, however, is that I don't know how to send an argument to the base class. Here is what I mean:
public class YourContext : DbContext {
public YourContext() : base("yourNameOrConnectionString")
{
}
}
The problem is that this code does not work:
string dbConnectionString = "MyDBTest";
using (var db = new YourContext(dbConnectionString))
{
//code to use the db
}
How does one send an argument to a base class, in this case YourContext() : base()
Use the config transforms. That's what they're there for. Admittedly, though, the config transforms are a little confusing because the debugger doesn't actually use them. Let me elaborate.
By default, you get three web.configs: Web.config
, Web.Debug.config
and Web.Release.config
. The latter two, are combined with the first in the Solution Explorer, but you can expand Web.config
to see them. Web.Debug.config
and Web.Release.config
are the transforms. They use a special XML-style syntax to allow you alter, or transform, settings in the main Web.config
file. Each corresponds to a "Configuration", namely "Debug" and "Release", the two built into Visual Studio by default. You can add additional configurations as you need them.
Now, here's where things are confusing. The Debug
configuration, despite its name, is never actually used when debugging. A better name would be perhaps Development
or Staging
; it's the configuration intended for when you deploy the site in a testing capacity, rather than to production. The Release
config is for production. So, what you need is in your main Web.config
, specify the connection string for your local development database. Then, in the Debug
and Release
configs, you add a transform to change that to the staging/production database connection strings, respectively. When you're debugging locally, the one the main web.config
will be used, and then when you publish your application, you'll choose to either use Debug
or Release
, based on the environment you'll be deploying to, and then the transforms will be run to alter the published web.config
appropriately.
For more information on transforms and how to write them, see: http://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx
这篇关于如何使用实体框架5在测试和生产数据库之间切换5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!