none
After creating and deleting a database, I can't access to a new database with same connection string RRS feed

  • Question

  • I have the following code where I create a database, add a record to it and then delete it:

    var connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=initcat;Integrated Security=True;";
    
            Database.SetInitializer(new DropCreateDatabaseAlways<ProjectContext>());
            using (var dbc = new ProjectContext(connectionString))
            {
                // add a record
                dbc.SaveChanges();
            }
            Database.Delete(connectionString);

    Everything works fine. However, when the following code

     Database.SetInitializer(new DropCreateDatabaseAlways<ProjectContext>());
            using (var dbc = new ProjectContext(connectionString))
            {           
                // add a record
                dbc.SaveChanges();
            }
    is executed after the code above in the same program, a System.Data.Entity.Core.EntityException wirh message: "The underlying provider failed on Open." appears. What could the cause be?
    Thank you.

    • Edited by Lucen97 Wednesday, August 21, 2019 12:50 PM
    • Moved by CoolDadTx Wednesday, August 21, 2019 2:02 PM EF related
    Wednesday, August 21, 2019 10:20 AM

All replies

  • Hello,

    Can you tell us what the InnerException is?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, August 21, 2019 11:09 AM
  • By printing the InnerException I get:

    System.Data.SqlClient.SqlException (0x80131904): Non è possibile aprire il database "initcat" richiesto dall'account di accesso. L'accesso non è riuscito.
    L'acccesso non è riuscito per l'utente 'LAPTOP-AEF0PD9T\HEW15BS514NL'.
       at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
       at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
       at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
       at System.Data.SqlClient.SqlConnection.Open()
       at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)
       at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
       at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
       at System.Data.Entity.Core.EntityClient.EntityConnection.<Open>b__2()
       at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
       at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
       at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
       at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
    ClientConnectionId:cf20a3ae-9301-4205-8f24-b1e67f071448
    Error Number:4060,State:1,Class:11

    Wednesday, August 21, 2019 11:50 AM
  • There is no inner.exception message.

    You can post to the MSDN EF forum for help.

    Wednesday, August 21, 2019 1:18 PM
  • There is no inner.exception message.

    You can post to the MSDN EF forum for help.

    Ok, I will do that, thank you.

    Wednesday, August 21, 2019 1:59 PM
  • Hi Lucen97,

    After calling Delete(String), it will delete the database on the database server if it exists.

    The code "var dbc = new ProjectContext(connectionString)" will not create a new database.

    And an exception will be thrown when you access the database again through the connection string.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 22, 2019 5:39 AM
    Moderator
  • There is no inner.exception message.

    You can post to the MSDN EF forum for help.

    What is the MSDN EF forum if not this one?  Its name is ADO.NET Entity Framework and LINQ to Entities.
    Thursday, August 22, 2019 5:29 PM