EF: The underlying provider failed on Open

asp.net azure c# entity-framework

Question

In production I have started getting this error all across my app:

The underlying provider failed on Open

As far as I could tell, I'm not getting this error when testing the app locally (at least I could not get it no matter what I did). I'm using Azure SQL server and here is my connection string:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=myserver.windows.net,1433;Database=mydb;User ID=username;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

Here is one of methods where I'm getting error:

public IEnumerable<Country> GetCountries()
{
        List<Country> countries;
        using (var databaseContext = new Database())
        {
            countries = databaseContext.Countries.OrderBy(c => c.Name).ToList();
        }

        return countries;
}

Also I want to stress out that error is not happening all the time, it happens kinda randomly. This started to happen today, I have not changed this code, nor connection sting, nor database settings, but (!) I have refactored some methods from synchronous to asynchronous - I don't know this is somehow connected to this particular problem.

Also as far as I could tell this is only happening in ASP.net Web API project, but not in ASP:net MVC project (they share same service layer and call same methods from service)

And finally here is Pastebin link to whole stack trace (I won't paste it here because it is too big): link

1
3
10/29/2015 6:09:52 PM

Popular Answer

Please see this post: Error 'The underlying provider failed on Open'.

The excerpt from the post that should resolve your issue is below:

Changing my code to the following fixed it:

using (DatabaseEntities context = new DatabaseEntities())
{
    context.Connection.Open();
    // the rest
}
3
5/23/2017 12:16:44 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow