Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

efbundle tries to make a connection to the OnConfiguring connection string despite using --connection parameter #27510

Closed
vIceBerg opened this issue Feb 25, 2022 · 13 comments

Comments

@vIceBerg
Copy link

Hi!

My connection string is actually hardcoded in the OnConfiguring method of my DbContext class

OnConfiguring method

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
	if (!optionsBuilder.IsConfigured)
	{
		string connectionString = $"server=my-dev-server;user=my-dev-user;password=my-dev-password;database=my-dev-database";
		ServerVersion version = Microsoft.EntityFrameworkCore.ServerVersion.Parse("5.7.36-mysql");

		optionsBuilder
			.UseMySql(connectionString, version)
			.UseSnakeCaseNamingConvention();
	}
}

Then I use this command to generate the efbundle

 dotnet ef migrations bundle      --verbose --context MyContext --force

When I execute the bundle on another computer with this command

.\efbundle.exe --verbose --connection 'server=my-prod-server;user=my-prod-user;password=my-prod-password;database=my-prod-database'

The other computer does not have access to my-dev-server, so I get this error:

Migrating using database 'my-prod-database' on server 'my-prod-server'.
Opening connection to database '' on server 'my-dev-server'.
An error occurred using the connection to database '' on server 'my-dev-server'.

If I run the bundle on my-dev-server but change the database like this:

.\efbundle.exe --verbose --connection 'server=my-dev-server;user=my-dev-user;password=my-dev-password;database=my-dev-database-2'

The my-dev-database-2 gets created as expected on my-dev-server

Here's the complete stack trace

Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'MyContextMigrationFactory'.
Found DbContext 'MyContext'.
Finding application service provider in assembly 'MyProject.DataModel'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Using DbContext factory 'MyContextMigrationFactory'.
Using context 'MyContext'.
Finding design-time services referenced by assembly 'MyProject.DataModel'...
Finding design-time services referenced by assembly 'MyProject.DataModel'...
No referenced design-time services were found.
Finding design-time services for provider 'Pomelo.EntityFrameworkCore.MySql'...
Using design-time services from provider 'Pomelo.EntityFrameworkCore.MySql'.
Finding IDesignTimeServices implementations in assembly 'MyProject.DataModel'...
No design-time services were found.
Migrating using database 'my-prod-database' on server 'my-prod-server'.
Opening connection to database '' on server 'my-dev-server'.
An error occurred using the connection to database '' on server 'my-dev-server'.
'MyContext' disposed.
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call.
 ---> MySqlConnector.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
   at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, MySqlConnection connection, Int32 startTickCount, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 433
   at MySqlConnector.MySqlConnection.CreateSessionAsync(ConnectionPool pool, Int32 startTickCount, Nullable`1 ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlConnection.cs:line 926
   at MySqlConnector.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlConnection.cs:line 406
   at MySqlConnector.MySqlConnection.Open() in /_/src/MySqlConnector/MySqlConnection.cs:line 369
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlRelationalConnection.Open(Boolean errorsExpected)
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   --- End of inner exception stack trace ---
   at Pomelo.EntityFrameworkCore.MySql.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 Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlDatabaseCreator.Exists(Boolean retryOnNotExists)
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.ExecuteInternal(String[] args)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.<>c__DisplayClass6_0.<Configure>b__0(String[] args)
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.Execute(String context, Assembly assembly, Assembly startupAssembly, String[] args)
An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call.

I know there's issues like #26869 that talks about using empty or null connection string in the OnConfigure methods, but I cannot find any talking about making a connection to another server than the one specified in the provided command line connectionstring.

Provider and version information

dotnet version: 7.0.100-preview.1.22110.4
EF Core version: 6.02
Database provider: Pomelo.EntityFrameworkCore.MySql
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.0.4

@vIceBerg vIceBerg changed the title efbundle tries to make a connection to the hardcoded connection string despite using --connection parameter efbundle tries to make a connection to the OnConfiguring connection string despite using --connection parameter Feb 25, 2022
@ajcvickers
Copy link
Member

@lauxjpn Looks like this could be a provider issue. Does the Pomelo provider try to use the connection string passed to UseMySql before the application attempts to open a connection?

/cc @bricelam

@bricelam
Copy link
Contributor

IIRC, The Pomelo provider needs to get the MySQL version during configuration, so yes, it connects to the database to do that.

@bricelam
Copy link
Contributor

bricelam commented Feb 28, 2022

Hmm, never mind. Looks like UseMySql takes the server version now.

@vIceBerg
Copy link
Author

vIceBerg commented Feb 28, 2022 via email

@ajcvickers
Copy link
Member

@vIceBerg We will investigate this, but please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing. At the moment, I can't get this to reproduce in projects I have tried.

@vIceBerg
Copy link
Author

vIceBerg commented Mar 2, 2022

Hi!

There's the project. I included a README file with the commands I used and the log it produces

Bug.zip

@ajcvickers
Copy link
Member

@roji to try with Pomelo.

@devenchawlayyc
Copy link

I am also facing the exact same issue with EF Core Bundles and Pomelo, any ETA on this @ajcvickers ?

@kjbkjb
Copy link

kjbkjb commented Mar 8, 2022

I have encountered this exact scenario using Pomelo and efbundle. My DbContext tries to get its connection string configuration first from appconfig, then overwrites from environment variables that match the path in appconfig. It was awkward for me to have efbundle executable require a connection parameter and contributed to my confusion when encountering this problem.

@ajcvickers
Copy link
Member

@roji Ping.

@kjbkjb
Copy link

kjbkjb commented Mar 9, 2022

I pulled together a small example based on my implementation using DbContextFactory with environment variables. Hope it helps, disregard if it does not.
efbundlepomelo.zip

@roji
Copy link
Member

roji commented Mar 14, 2022

@bricelam @ajcvickers @lauxjpn this repros for me for MySQL (EF Core 6.0) with @vIceBerg's repro above, but does not repro when switching to SQL Server. This does smell similar to npgsql/efcore.pg#1876 on the PG side, but the above did not repro for Npgsql.

@lauxjpn at this point this does look like a MySQL-specific issue - do you want to take a look? If you need help let us know, one of us can also take a look.

@ajcvickers
Copy link
Member

Moved to PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#1626

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants