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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bonomi
  • 2,541
  • 5
  • 39
  • 51
  • possible duplicate of [Configure Entity Framework to pick connection strings from Azure CloudConfiguration File?](http://stackoverflow.com/questions/24222862/configure-entity-framework-to-pick-connection-strings-from-azure-cloudconfigurat) – Abhijeet Oct 10 '14 at 12:52

4 Answers4

2

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

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

}
Bonomi
  • 2,541
  • 5
  • 39
  • 51
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.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
  • 1
    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 – Bonomi Oct 10 '14 at 13:32
  • 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. – Brendan Green Oct 10 '14 at 13:41
  • WebRole or WorkerRole? Version of EF? – Brendan Green Oct 11 '14 at 03:28
  • I am using EF6 and WorkerRole – Bonomi Oct 11 '14 at 17:25
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 );
David Turvey
  • 2,891
  • 6
  • 27
  • 29
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>
Community
  • 1
  • 1
Cameron Aavik
  • 812
  • 7
  • 21