Skip to content

DatabaseFacade.EnsureCreatedAsync and DatabaseFacade.EnsureDeleted fail on SQL Server if connection string contains a password #9960

Closed as not planned

Description

@imb590

If DbContext is created with SqlConnection having an explicit password in the connection string, and the database exists, the method EnsureDeleted fails on its DatabaseFacade:

static void Main()
{
    var connection = new SqlConnection(@"Data Source=.\server;Initial Catalog=DbName;User Id=user;Password=password");
    var options = new DbContextOptionsBuilder<DbContext>()
        .UseSqlServer(connection)
        .Options;
    var ctx = new DbContext(options);
    ctx.Database.EnsureDeleted();
}
Exception message: System.Data.SqlClient.SqlException : Login failed for user 'user'.
Stack trace:
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
   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.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerDatabaseCreator.Delete()
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureDeleted()
   at Main()

It seems the password gets removed from the connection string somewhere, and connection created by SqlServerConnection.CreateMasterConnection is left with an incomplete connection string.

The method EnsureCreatedAsync also fails for this DbContext instance if database does not exist.

Activity

  1. ajcvickers commented on Oct 4, 2017

    @ajcvickers
    Contributor

    @MBashov When EF needs to create a connection to the master database it effectively has to clone the connection string. For this to work, the password, etc needs to not be stripped from the connection string, which can be done by adding "Persist Security Info=True". Does it work if you set this in your connection string?

  2. ajcvickers commented on Oct 4, 2017

    @ajcvickers
    Contributor

    Closing this as it only happens in cases where EF has to use the master database, which are the Ensure... methods (designed for prototyping/testing rather than production), the workaround is reasonable, and there are no great ways that we are aware of to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions