hyperN
hyperN

Reputation: 2754

EF: The underlying provider failed on Open

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

Upvotes: 3

Views: 11616

Answers (3)

Rajeshwar
Rajeshwar

Reputation: 1

I got the same error while testing in Azure. In my case, I forgot to update the password in the application web.config. Post updating its working fine for me.

Upvotes: 0

Igor Levashov
Igor Levashov

Reputation: 328

Add connection string to your dbcontext constructor like `

public YourDbContext()
          :base(name="YourDbContext")
{
this.Database.Connection.ConnectionString = "Data Source=****;Initial Catalog=****;User ID=****;Password=****";
}

Upvotes: 0

Joe Raio
Joe Raio

Reputation: 1805

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
}

Upvotes: 3

Related Questions