Bug #119419 MySql.EntityFrameworkCore error connecting with MySqlConnection when running migrations
Submitted: 18 Nov 10:30
Reporter: James Britton Email Updates:
Status: Open Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:9.0.9 OS:Any
Assigned to: CPU Architecture:Any
Tags: dotnet, migrations, NuGet Version 9.0.9

[18 Nov 10:30] James Britton
Description:
When I set up a connection to a MySQL database in dotnet using the MySql.EntityFrameworkCore package I get an error when running migration when I setup the connection in a specific way.

This way I get a connection error when it attempts to run migrations. It seems to create the EFMigrationHistroy table but then exits with a connection error.

.UseMySQL(new MySqlConnection("conString"), opt => opt.MigrationAssembly(aMigationAssembly))

When I change it to use the connection string directly this doesn't happen.

.UseMySQL("conString", opt => opt.MigrationAssembly(aMigationAssembly))

How to repeat:
Using an 8.4.5 MySQL database (have also tried on 9.5.0)

Using a .NET project with version net9.0 or net10.0 that has EF Core migration to run.

And set up a user inthe MySQL server with permissions to create databases.

1. Set upa  connection to the database in your migration project using .UseMySQL() extension passing an instance of MySqlConnection constructed using a connection string.

2. Apply migrations with this project using the 'dotnet ef database update' command.

3. You should see the following error:

'MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host 'localhost' for user 'docker' using method 'caching_sha2_password' failed with message: Access denied for user 'docker'@'192.168.65.1' (using password: NO)
 ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user 'docker'@'192.168.65.1' (using password: NO)
   at MySql.Data.MySqlClient.MySqlStream.ReadPacketAsync(Boolean execAsync)
   at MySql.Data.MySqlClient.NativeDriver.ReadPacketAsync(Boolean execAsync)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacketAsync(Boolean execAsync)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(MySqlException ex)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacketAsync(Boolean execAsync)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticateAsync(Boolean reset, Boolean execAsync)
   at MySql.Data.MySqlClient.NativeDriver.AuthenticateAsync(String authMethod, Boolean reset, Boolean execAsync)
   at MySql.Data.MySqlClient.NativeDriver.OpenAsync(Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.NativeDriver.OpenAsync(Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.Driver.OpenAsync(Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.Driver.CreateAsync(MySqlConnectionStringBuilder settings, Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.Driver.CreateAsync(MySqlConnectionStringBuilder settings, Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnectionAsync(Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnectionAsync(Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriverAsync(Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.MySqlPool.GetConnectionAsync(Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Boolean execAsync, CancellationToken cancellationToken)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at MySql.EntityFrameworkCore.Storage.Internal.MySQLDatabaseCreator.<>c__DisplayClass16_0.<Exists>b__0(DateTime giveUp)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext _, TState s)
   at MySql.EntityFrameworkCore.Storage.Internal.MySQLExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
   at MySql.EntityFrameworkCore.Storage.Internal.MySQLDatabaseCreator.Exists(Boolean retryOnNotExists)
   at MySql.EntityFrameworkCore.Storage.Internal.MySQLDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrations()
   at MySql.EntityFrameworkCore.Migrations.Internal.MySQLMigrator.MigrateImplementation(DbContext context, String targetMigration, MigrationExecutionState state, Boolean useTransaction)
   at MySql.EntityFrameworkCore.Migrations.Internal.MySQLMigrator.<>c.<Migrate>b__20_1(DbContext c, ValueTuple`4 s)
   at MySql.EntityFrameworkCore.Storage.Internal.MySQLExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at MySql.EntityFrameworkCore.Migrations.Internal.MySQLMigrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)'

4. Change the .UseMySQL call to pass in connection string directly, the error doesn't occur.