Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework : "The underlying provider failed on Open"

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 I get this Application Error : Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed. [CLIENT: ]

<add name="ASPNETDBEntities"
     connectionString="
         metadata=res://*/Models.FriendList.csdl|res://*/Models.FriendList.ssdl|res://*/Models.FriendList.msl;
         provider=System.Data.SqlClient;
         provider connection string=&quot;
         Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;
         Integrated Security=True;
         Connect Timeout=30;
         User Instance=True;
         MultipleActiveResultSets=True&quot;"
     providerName="System.Data.EntityClient" />

I am using aspnetdb.mdf file, and not any external database. I have searched enough for this, but no use.

Everything works fine with VWD webserver

like image 519
pokrate Avatar asked May 16 '10 19:05

pokrate


3 Answers

You should create a sql account for your web server to access the aspnetdb database. It is currently using integrated authentication (tries to logon with the identity the web server is using to run the application).

The example below uses integrated auth. I would use SQL auth though.

http://msdn.microsoft.com/en-us/library/ff649314.aspx

like image 109
Raj Kaimal Avatar answered Nov 17 '22 01:11

Raj Kaimal


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

like image 3
Adeel Rizvi Avatar answered Nov 17 '22 01:11

Adeel Rizvi


Using new instance of DB Entity on every action should not physically create connection to your SQL server. Entity Framework will use connection pool created for your (process, app domain, connection string) as configured in your connection string to avoid creating new connections.

This issue is very environmental and tweaking parameters (in your conn string) like below should resolve the problem-

Min Pool Size=1;

Max Pool Size=100; // default

Connect Timeout=15; // in seconds

Pooling=true;

like image 2
Suneet Nangia Avatar answered Nov 17 '22 01:11

Suneet Nangia