7

I've been working on upgrading an ASP.NET 5 RC1 app to ASP.NET Core 1. I have successfully upgraded it by replacing packages (AspNet to AspNetCore) and changing from EF7 to EF Core (Microsoft.Data.Entity to Microsoft.EntityFrameworkCore).

The issue I'm having is an intermittent SqlException for Connection Timeout Expired.

Here is the complete exception:

System.Data.SqlClient.SqlException occurred.  
Class=11 ErrorCode=-2146232060 HResult=-2146232060 LineNumber=0  
**Message=Connection Timeout Expired.  The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement.  This could be because the pre-login handshake failed or the server was unable to respond back in time.  The duration spent while attempting to connect to this server was - [Pre-Login] initialization=924; handshake=6;**   
  Number=-2   
  Procedure=""  
  Server=(localdb)\mssqllocaldb  
  Source=.Net SqlClient Data Provider  
  State=0  
  StackTrace:  
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)  

I have tried increasing the command timeout like so:

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
    this.Database.SetCommandTimeout(120);
}

The exception is thrown on this line, but when I comment it out it will happen elsewhere which leads me to believe it happens the first time the database is accessed.

app.ApplicationServices.GetService<ApplicationDbContext>().Database.Migrate();
2
  • now no issue or what ? after this this.Database.SetCommandTimeout(120); Commented Sep 12, 2016 at 17:53
  • The issue still happens even after adding the timeout. Commented Sep 12, 2016 at 18:01

1 Answer 1

9

Your issue is related to the amount of time it takes for a connection to be established with the server. Altering the CommandTimeout value (the amount of time to allow a command to take to execute) will not have any effect on that. You need to consider increasing the connection timeout value, which is achieved via the connection string:

Server=(localdb)\mssqllocaldb;Database=yourDb;Trusted_connection=true;connect timeout=100;
Sign up to request clarification or add additional context in comments.

2 Comments

Do you know why I haven't run into this until after upgrading to Core 1.0?
It has nothing to do with versions of Entity Framework. The issue is related purely to the responsiveness of your SQL Server. LocalDb spins up on demand. I have no idea what kinds of factors can affect he amount of time it takes LocalDb to get itself ready, but the version of EF or .NET that you use is highly unlikely to be one of them.

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.