How to reduce connection timeout with Entity Framework?

I want the database connection to my SQL Server to be fast. How to reduce the timeout? I tried adding Connection Timeout=1to the connection string, but that doesn't seem to matter.

With Connection Timeout=500, it takes about 8 minutes 30 seconds to timeout. It is expected. Using Connection Timeout=1the timeout takes about 40 seconds, which is much longer than expected.

I found the property EntityConnection.ConnectionTimeout, but it is read-only. Is there anything else I can do to reduce this timeout? Is this a problem with Entity?

UPDATE: Here is my connection string. ~ 40 seconds are still required before a timeout.

<add name="KofaxAdminToolsEntities" connectionString="metadata=res://*/DB.Model.KofaxAdminTools.csdl|res://*/DB.Model.KofaxAdminTools.ssdl|res://*/DB.Model.KofaxAdminTools.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MY_DATASOURCE;initial catalog=MY_CATALOG;persist security info=True;user id=MY_USER;password=MY_PASSWORD;MultipleActiveResultSets=True;App=EntityFramework;Connection Timeout=1&quot;" providerName="System.Data.EntityClient" />

UPDATE 2: . Thus, I found that the timeout was tcp timeout , not sql connect timeout . If my machine could reach the host, @marc_s's solution would work, however, since I cannot reach this host, tcp timeout comes into play. Does anyone know how to reduce tcp timeout for SqlConnection?

+5
source share
2 answers

Where and how did you specify this connection timeout?

I just tried and added this to my EF connection string (using the EF database first) - and it works as expected: when the SQL Server service stops, the connection time almost immediately disappears ....

<add name="myEntities" 
     connectionString="metadata=res://*/People.csdl|res://*/People.ssdl|res://*/People.msl;provider=System.Data.SqlClient;
             provider connection string=&quot;data source=.;initial catalog=mydb;
             integrated security=True;connect timeout=1;multipleactiveresultsets=True;
                                      ***************** 
             App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

( EF - web.config)

connect timeout=x ( x , 0) provider connection string= EF.

+1

DbContext:

((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 1;
0

All Articles