Repository navigation
Closed as not planned
Closed as not planned
Copy link
Description
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.
Metadata
Metadata
Assignees
Labels
No labels
Activity
ajcvickers commentedon Oct 4, 2017
@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?
ajcvickers commentedon Oct 4, 2017
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.