locked
The Underlying Provider failed on Open... RRS feed

  • Question


  • Hey everyone.  I wanted to give EF a shot instead of using some other means to develop my Data Access Layer of my application... so, I created my EDM and generated a database in my SQL Server 2008 Express edition, which is installed locally on my laptop.

    When I attempted to use the SaveChanges() method in my Container, I received the error in my subject.  I continued to run my WebApp until I got the error page, which had the following details/stack.


    Exception Details: System.Data.SqlClient.SqlException: Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\MyDB.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".
    An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ MyDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    Source Error: 

    Line 14:     action.action = "Driving"
    Line 15:     container.AddToActions(action)
    Line 16:     container.SaveChanges()
    Line 17: 
    Line 18:   End Sub

    Source File: C:\Documents and Settings\username\My Documents\Visual Studio 2010\Projects\MyApp\MyApp\Search.aspx.vb    Line: 16 

    Stack Trace: 

    [SqlException (0x80131904): Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\MyDB.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".
    An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\MyDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.]
      System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009598
      System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
      System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
      System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +35
      System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) +183
      System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) +239
      System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +195
      System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +232
      System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +185
      System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +33
      System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +524
      System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
      System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +479
      System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +108
      System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
      System.Data.SqlClient.SqlConnection.Open() +125
      System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) +81
    
    [EntityException: The underlying provider failed on Open.]
      System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) +273
      System.Data.EntityClient.EntityConnection.Open() +184
      System.Data.Objects.ObjectContext.EnsureConnection() +140
      System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) +635
      System.Data.Objects.ObjectContext.SaveChanges() +38
      MyApp.WebForm1.btnAdd_Click(Object sender, EventArgs e) in C:\Documents and Settings\username\My Documents\Visual Studio 2010\Projects\MyApp\MyApp\Search.aspx.vb:16
      System.Web.UI.WebControls.Button.OnClick(EventArgs e) +141
      System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +149
      System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +39
      System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +37
      System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +87
      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4225
    


    I saw there was another post on this forum some ages ago found here but it was related to SQL Compact Edition.  Does anyone have an idea about this problem and how I can resolve this issue?  THanks in advance.  :)

    Wednesday, April 28, 2010 7:15 PM

Answers

  • Hello,

    Thanks for your post!

    This error usually happens because there are multiple connections if you use Transactions. Please try to manually open and close the connection to database:http://msdn.microsoft.com/en-us/library/bb738698.aspx

    I found a similar thread in asp.net forum. As you can see, on some scenarios, close the connection in server explorer in Visual Studio could solve the issue.

    Please have a try and let me know if this help. If it persists, please feel free to post a code snippet here for further discussion.

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Tuesday, May 4, 2010 10:12 AM