1

I've created an ASP.NET MVC project that uses Entity Framework and Identity.

The problem is that when I debug my project, I get an error.

(note: If I delete my DB from SQL Server Object Explorer and run it again this error don't show up, but I don't want to manually delete it every time)

Error:

Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=15; handshake=3; [Login] initialization=0; authentication=2; [Post-Login] complete=14991

Connection string:

<add name="MovieReviewsDb" 
     providerName="System.Data.SqlClient" 
     connectionString="Data Source(LocalDb)\v11.0;AttachDbFileName=|DataDirectory|\MovieReviewsDb.mdf;Initial Catalog=MovieReviewsDb;Integrated Security=SSPI;MultipleActiveResultSets=True" />   

I've set custom initializer that inherits from DropCreateDatabaseAlways. I have overridden the Seed method where I populate the database with 3 records and creates default roles and users (Identity).

(note: my DbContext inherits from IdentityDbContext)

I'm stuck at this for several hours and searched through a lot of similar posts (like this one) but I can't find an answer.

Is it possible to set a bigger timeout (maybe It takes too long and the timeout is reached) and how?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
stubborn
  • 180
  • 1
  • 4
  • 14
  • Your connection string doesn't seems to be a valid connection string for Identity... The connection strings for Identity are specific and looks like `metadata=res://*/Entities.csdl|res://........` – Manta Nov 05 '18 at 21:45

2 Answers2

0

I think I've finally figured it out.

I've added ;Connection Timeout=120; to my connection string and it works. I guess it just needed more time.

stubborn
  • 180
  • 1
  • 4
  • 14
0

There are two different solutions increase the timeout or retry policy for network problems

  1. Connection timeout

Change the connection string as below.

"ConnectionString": "...;Connection Timeout=120;"
  1. Retry policy

Refactoring code in manner so that it would allow you to call the query recursively till you get a desired result.

for example Polly retry policy

Re-try to open a SQL Server connection if failed

mesutpiskin
  • 1,771
  • 2
  • 26
  • 30