I apologize in beforehand because I see that a lot of people have already answered this question but still I cannot find the answer I am looking for.

In my solution I got 5 projects where 2 are C# libraries (the Core layer and the Data layer). The other 3 are using the Core layer to connect to the data layer and these three are a web-API project, a test winform project and a MVC project. I am currently trying out the winform project and that is where the error occurs. The API project works fine when connecting to database.

As the title suggest I have gotten this common exception in the winform project

System.Data.DataException: 'An exception occurred while initializing the database. See the InnerException for details.'

EntityException: The underlying provider failed on Open.

with the following inner exceptions

SqlException: 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=38878; handshake=35;

Win32Exception: The wait operation times out

The errors occurs in my Datacontext on Database.Initialize(true);:

    public class DataContext : DbContext
        public DataContext() : base("HololensRegistreringsskyltar")
            Database.SetInitializer(new DataContextInitializer<DataContext>());

I am also using generic repository pattern with Unit Of Work if that matters (and I am new to it).

Btw there is only a connection string in the API projects web.config, maybe there should be in the other two "outer" layers too?

Can anyone tell me how to solve this issue and making it work for all 3 "outer" layers?

1 Answers

George Human On

Following cases can cause this exception:

  • An instance of the SQL Server Database Engine is not running.
  • The SQL Server Browser service is not running.
  • The TCP/IP is disabled.
  • The server name was typed incorrectly.
  • There are network problems.
  • The TCP/IP port for the Database Engine instance is blocked by a firewall.
  • The client and server are not configured to use the same network protocol.