问题
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?
回答1:
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.
回答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.
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.
来源:https://stackoverflow.com/questions/11199958/is-this-how-to-set-context-connection-string-using-cloudconfigurationmanager