title: A network-related or instance-specific error occurred while establishing a connection to SQL Server - ASP.NET MVC Entity Framework
body: <p>I am using ASP.Net MVC 3 (Entity Framework) with Sql server 2008. Connection string is fine in web.config. But sometimes i get the following exception from my application:</p>
  
  <pre><code>    System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
  
  Generated: Wed, 02 May 2012 05:05:21 GMT
  
  System.Data.EntityException: The underlying provider failed on Open. ---&gt; System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
     at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity)
     at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
     at System.Data.SqlClient.SqlInternalConnectionTds.LoginWithFailover(Boolean useFailoverHost, ServerInfo primaryServerInfo, String failoverHost, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
     at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
     at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
     at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
     at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
     at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
     at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
     at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
     at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
     at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
     at System.Data.SqlClient.SqlConnection.Open()
     at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean&amp; closeStoreConnectionOnFailure)
     --- End of inner exception stack trace ---
     at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean&amp; closeStoreConnectionOnFailure)
     at System.Data.EntityClient.EntityConnection.Open()
     at System.Data.Objects.ObjectContext.EnsureConnection()
     at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
     at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator()
     at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
     at System.Data.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__2[TResult](IEnumerable`1 sequence)
     at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
     at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
     at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
     at MyApp.Data.UserRepository.GetUserDetails(String username)
     at MyApp.Core.Services.SiteService.GetUserDetails(String username, Int64 userId)
     at MyApp.PublicSite.Web.Controllers.BaseController.Initialize(RequestContext requestContext)
     at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
     at MyApp.PublicSite.Web.Controllers.BaseController.Execute(RequestContext requestContext)
     at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
     at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClass6.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5()
     at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0()
     at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
     at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d()
     at System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f)
     at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
     at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
     at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
     at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
     at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>The Connection string is:</p>
  
  <pre><code>    &lt;connectionStrings&gt;
      &lt;add name="MyAppDBContainer" connectionString="metadata=res://*/MyAppDB.csdl|res://*/MyAppDB.ssdl|res://*/MyAppDB.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=SERVERNAME;initial catalog=MyAppDB;persist security info=True;user id=USERID;Password=PASSWORD;multipleactiveresultsets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt;
    &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>This exception occurs randomly and not able to get/reproduce it everytime. Any possible settings i may be missing here? Please suggest.</p>