Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this how to set Context Connection String Using CloudConfigurationManager?

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.

I was configuring my context like this

public DomainContext()
    : base("ContextConnectionString")

This was taking the ContextConnectionString from the Web.config

I changed the Context Constructor to 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?

like image 538
GraemeMiller Avatar asked Jun 26 '12 02:06

GraemeMiller


Video Answer


2 Answers

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.

like image 146
knightpfhor Avatar answered Nov 07 '22 19:11

knightpfhor


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.

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.

like image 40
mcollier Avatar answered Nov 07 '22 19:11

mcollier