0

I'm trying to connect to Azure SQL, but I get error The underlying provider failed on Open

I discovered that I have put connection string in the wrong app.config - moved it to the executable project's app.config, but still the same result

When I check ConfigurationManager.ConnectionStrings["AzureDatabase"] it returns null. And when I try to connect it just uses default SQLExpress.

When I check build folder of my executable application - there is an <app_name>.exe.config file with "AzureDatabase". I'm stuck on where to search from here

This is my DbContext class

[DbConfigurationType(typeof(AzureDbConfiguration))]
public class ProductDbContext : DbContext
{
    //Db sets

    static MyContext()
    {
        //I don't want to change database from code
        Database.SetInitializer<MyContext>(null);
    }

    public MyContext() : base("AzureDatabase") //this is my connectionstring
    {            
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //some mappings
    }
}

This is AzureDbConfiguration

public class AzureDbConfiguration : DbConfiguration
{
    public AzureDbConfiguration()
    {
        SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy(2, TimeSpan.FromSeconds(10)));
    }
}

This is my app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
    <connectionStrings>
        <add name="AzureDatabase" 
             connectionString="Server=tcp:xxx.database.windows.net,1433;Initial Catalog=xxx;Persist Security Info=False;User ID=xxxx;Password=xxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient"/>
            </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />     
    </providers>
  </entityFramework>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

Any ideas on what's wrong here?

Prokurors
  • 2,458
  • 3
  • 40
  • 65
  • You named your connection string `AzureDatabase` and are checking `ConfigurationManager.ConnectionStrings["MyConnectionString"]`? Doesn't that naming not align? Does it work if you name it `DefaultConnection`? – Dan Rediske Aug 31 '16 at 18:47
  • @drediske Ah, sorry - it is `AzureDatabase` instead of `MyConnectionString`. Updated my post... – Prokurors Sep 01 '16 at 07:11

1 Answers1

0

It turns out the problem was that I was trying to work with app.config from Class library (I was trying to create an integration tests)

One of potential solutions is to create a separate settings file for Class library. You can read about it in this StackOverflow post

Community
  • 1
  • 1
Prokurors
  • 2,458
  • 3
  • 40
  • 65