使用EF 4.1/DbContext根据环境切换连接字符串 [英] Switch connection strings based on environment with EF 4.1/DbContext

查看:71
本文介绍了使用EF 4.1/DbContext根据环境切换连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了几篇有关此问题的文章,但无法从响应中创建可用的解决方案.也许是由于缺乏了解.

I have seen several posts about this but have not been able to create a usable solution from the responses. Perhaps due to a lack of understanding.

提供的托管服务要求在登台和生产时使用相同的代码库,包括连接字符串.

The hosting provided requires that an identical code base be used on staging and production, including connection string.

如何切换DbContext的连接字符串?

How do I switch the connection string for DbContext?

我知道我可以做这样的事情:

I understand I can do something like this:

public FooEntities() : base("ApplicationServices") { }

但这不是动态的-它只是在运行时设置它.

But this is not dynamic - it merely sets it at runtime.

那么我该如何在运行时实际选择连接字符串?

So how would I actually CHOOSE the connection string at runtime?

推荐答案

public FooEntities() : base("ApplicationServices") { }

FooEntitiesObjectContext

您也可以写

public FooEntities() : base(YourStaticMethodToGetConnectionString()) { }

然后您可以根据某些环境设置从web.config中提取连接字符串

Then you could pull the connection string from the web.config based on some environment setting

这篇关于使用EF 4.1/DbContext根据环境切换连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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