1

I am using the entity-framework with Code First approach and I have a custom DbContext. My connectionstring is defined in the ServiceConfiguration.Cloud.cscfg and ServiceConfiguration.Local.cscfg files.

 public CustomContext()
                : base("dbActivity")
 {
    ...

When the above code executes, it tries to check my web.config and not the ServiceConfiguration file from an azure solution.

UPDATE: Here is my connectionString in the ServiceConfiguration file:

<ConfigurationSettings>
  <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
  <Setting name="dbActivity" value="Server=tcp:xxx.database.windows.net,1433;Database=ra;User ID=user;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" />
</ConfigurationSettings>

Exception:

No connection string named 'Server=tcp:xxx.database.windows.net,1433;Database=ra;User ID=user;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;' could be found in the application config file."

Is it possible to make it check the ServiceConfiguration.Local.cscfg instead?

1

4 Answers 4

2

Finally I found the solution. It's necessary to use the SqlConnectionStringBuilder class.

public RAActivityContext()
    : base((new SqlConnectionStringBuilder(
            CloudConfigurationManager.GetSetting("dbRAActivity")).ToString()))
{            

}
Sign up to request clarification or add additional context in comments.

Comments

0

There is an overloaded constructor that takes an actual connection string.

This should do the trick:

public CustomContext()
                : base(CloudConfigurationManager.GetSetting("dbActivity"))
{
    // stuff here
}

Of course, the setting contains the (full) connection string.

4 Comments

I tried this solution before, but I still get the same exception. It looks like it is only accepting the connection string name, not the connection string itself
What is the exception that is thrown? What version of EF are you using? What does the configuration in your ServiceConfiguration.Local.cscfg look like? I am doing the above and it is working fine with EF 6.
WebRole or WorkerRole? Version of EF?
I am using EF6 and WorkerRole
0

I don't know it you can directly load it from a different config but there is a constructor for dbcontext that takes a connection string as input.

string connectionString = LoadFromConfig();
var context = new CustomContext(connectionString );

Comments

0

I needed to do this, but wasn't able to change the actual DbContext constructor as it was in a shared library that was also used by projects that used an app.config. I ended out coming up with an alternative solution which was to programatically modify the app.config connection string inside the worker role itself.

See this for a way to do that: Change connection string & reload app.config at run time

The code I used was

string databaseConnectionString = CloudConfigurationManager.GetSetting("dbActivity");
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection) config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["dbActivity"].ConnectionString = databaseConnectionString;
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");

For this to work you need to have an empty connection string inside the app.config for which it will overwrite

<connectionStrings>
    <add name="dbActivity" connectionString="" providerName="System.Data.SqlClient" />
</connectionStrings>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.