ASP.NET Core with Entity Framework Core error, Login Failed

I was working with the EF NuGet package Microsoft.EntityFramework.Tools.DotNet and executed the command-line interface (CLI), also shown in Figure 1.

dotnet ef database drop

Running this method resulted in the same behavior:

context.Database.EnsureDeleted();

image

Figure 1, Entity Framework Core, CLI

I am not sure what exactly I was thinking, but it I did not really expect it to drop my entire database, even though the message was pretty clear in Figure 1.  For some reason I thought it was only going to remove the tables.  I likely got this impression because of what the context.Database.EnsureCreated() method does in my Initializer when my ASP.NET Core application starts… Nonetheless, when I then tried to update the database using this command, see Figure 2 in addition,

dotnet ef database update

What I realize now is that the when I am pointing to a local database and run context.Database.EnsureDeleted(), it drops the database, but when I then run context.Database.EnsureCreated() it creates it again, but the same is not so when you are pointing to an AZure SQL database.

image

Figure 2, cannot login to Entity Framework Core database hosted on Azure SQL via an ASP.NET Core Web Application, Login Failed

OK, fair is fair that I could not login, but perhaps stating the database does not exist at all would have moved me forward a bit faster.  SO, if you get this and nothing has changed, maybe your database got deleted….be sure to take a back up from time to time and check out this link here for more about managing EF migrations.

When I was developing my application and when it was pointed to the local database, when I ran the context.Database.EnsureCreated() it created a database, however when I was pointing to an Azure SQL instance, the database was not created.

context.Database.EnsureCreated()

Initially, I was logging in to the Azure portal and manually recreating the database each time I needed to update the database, until I found that I can run the following DOTNET command which recreates the database, there may be a better way, but this is what I found so far, maybe there is a CREATEDB command which I have not yet discovered, see Figure 3.

dotnet ef database update

image

Figure 3, don’t drop the database if you do then just use ef to recover, but the data is gone

The error I received is shown in Figure 4, and the output is below it in text format so you can check if the exception stack I got matches yours…

image

Figure 4, SqlException: Login failed for user ‘’, Entity Framework Core, SQL Azure

An error occurred while starting the application.
SqlException: Login failed for user ‘HealthFit’.
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, object providerInfo, bool redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, bool applyTransientFaultHandling)
• SqlException: Login failed for user ‘HealthFit’.
o   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, object providerInfo, bool redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, bool applyTransientFaultHandling)
o    System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
o System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
o   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
o System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
o    System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, uint waitForMultipleObjectsTimeout, bool allowCreate, bool onlyOneCheckConnection, DbConnectionOptions userOptions, out DbConnectionInternal connection)
o System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource<DbConnectionInternal> retry, DbConnectionOptions userOptions, out DbConnectionInternal connection)
o    System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource<DbConnectionInternal> retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, out DbConnectionInternal connection)
o System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource<DbConnectionInternal> retry, DbConnectionOptions userOptions)
o  System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource<DbConnectionInternal> retry)
o    System.Data.SqlClient.SqlConnection.Open()
o Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open()
o  Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerDatabaseCreator+<>c__DisplayClass11_0.<Exists>b__0(DateTime giveUp)
o    Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute<TState, TResult>(Func<TState, TResult> operation, Func<TState, ExecutionResult<TResult>> verifySucceeded, TState state)
o  Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute<TState, TResult>(IExecutionStrategy strategy, Func<TState, TResult> operation, TState state)
o Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureDeleted()
o    HealthAndFitness.Data.DbInitializer.Initialize(HealthAndFitnessContext context) in DbInitializer.cs