Entity Framework : “The underlying provider failed on Open”

前端 未结 6 1796
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 00:20

When I try to insert a record, I get this error : The underlying provider failed on Open. This error occurs only with IIS and not with VWD 2008\'s webserver. In the EventViewer

6条回答
  •  青春惊慌失措
    2021-02-13 00:42

    I was getting the same problem and after doing debugging i saw that i am creating the new instance of DB Entity on every action and making new instance of Db Entity means openning new connection with db.

    Below is the code :

    Private tmpConnection As New DbModel.DbEntities
    

    so by calling the variable again and again its creating new instance of DbEntites and opening new connection to db.

    so i write a small function for this and that solved my problem. Now no more error.

    Private tmpConnection As DbModel.DbEntities
    
    Public Function dbCon() As DbModel.DbEntities
    
        If tmpConnection IsNot Nothing Then
    
            If tmpConnection.Connection.State = ConnectionState.Closed Then
    
                Try
                    tmpConnection.Connection.Open()
                    Return tmpConnection
                Catch ex As Exception
                    My.Response.Redirect("dberror.aspx")
                End Try
    
            Else
    
                Return tmpConnection
    
            End If
    
        Else
    
            tmpConnection = New DbModel.DbEntities
    
            Try
                tmpConnection.Connection.Open()
                Return tmpConnection
            Catch ex As Exception
                My.Response.Redirect("dberror.aspx")
            End Try
    
        End If
    
        Return Nothing
    End Function
    

    and last thing in your connectionstring please add "Connect Timeout=30;"

    thats working so perfect for me

提交回复
热议问题