阅读背景:

这是如何使用CloudConfigurationManager设置Context Connection String的?

来源:互联网 

I am looking to use CloudConfigurationManager so I can take advantage of Azure configuration files. I want to use a connection string I added a string to my Cloud.cscfg to configure Entity Framework Context.

我希望使用CloudConfigurationManager,以便我可以利用Azure配置文件。我想使用连接字符串我在Cloud.cscfg中添加了一个字符串来配置实体框架上下文。

I was configuring my context like this

我正在配置我的上下文

public DomainContext()
    : base("ContextConnectionString")

This was taking the ContextConnectionString from the Web.config

这是从Web.config获取ContextConnectionString

I changed the Context Constructor to this

我将Context Constructor更改为this

public DomainContext()
    : base(CloudConfigurationManager.GetSetting("ContextConnectionString"))

And it now works.

它现在有效。

Is there a more elegant way to tell my context constructor to use Azure cscfg first?

是否有更优雅的方式告诉我的上下文构造函数首先使用Azure cscfg?

2 个解决方案

#1


10  

There is not a more elegant solution at the moment, no. In fact this is quite a bit more elegant than what you would have had to have written a month ago. The CloudConfigurationManager is a class that is new in the 1.7 SDK and was created because previously there was no built in support for doing this sort of thing. If you wanted to do what CloudConfigurationMangager does now you had to create your own class, which was pretty common.

目前没有更优雅的解决方案,没有。事实上,这比你一个月前写的要优雅得多。 CloudConfigurationManager是1.7 SDK中的新类,之所以创建是因为之前没有内置支持来执行此类操作。如果你想做CloudConfigurationMangager现在做的事情,你必须创建自己的类,这很常见。

#2


2  

Would you be able to post a sample of the code?

你能发布代码样本吗?

CloudConfigurationManager should first check if the code is executing in a Windows Azure role, and if so, attempt to retrieve the configuration value from the in the ServiceConfiguration.cscfg file (that is deployed with the role). If not running in a role, CloudConfigurationManager should revert to the application's .config (web.config or app.config) file. In either case, I believe NULL is returned if the value is not found.

CloudConfigurationManager应首先检查代码是否在Windows Azure角色中执行,如果是,请尝试从ServiceConfiguration.cscfg文件(使用该角色部署)中检索配置值。如果未在角色中运行,CloudConfigurationManager应恢复为应用程序的.config(web.config或app.config)文件。在任何一种情况下,我相信如果找不到值,则返回NULL。

With the connection string retrieve from the .cscfg file (assuming the code is running in a Windows Azure web role), the value could be passed to an EF constructor overload which would set the connection string.

通过从.cscfg文件检索连接字符串(假设代码在Windows Azure Web角色中运行),该值可以传递给EF构造函数重载,该重载将设置连接字符串。


分享到: