Pomelo.EntityFrameworkCore.MySql icon indicating copy to clipboard operation
Pomelo.EntityFrameworkCore.MySql copied to clipboard

`Cannot change the connection string on an open connection` from UseMySql

Open Coder3333 opened this issue 2 years ago • 7 comments

Steps to reproduce

Call UseMySql[TContext](DbContextOptionsBuilder`1 optionsBuilder, DbConnection connection, ServerVersion serverVersion, Action`1 mySqlOptionsAction) with a MySqlConnector.MySqlConnection (Cannot change the connection string on an open connection) or MySqlDataAlias.MySql.Data.MySqlClient.MySqlConnection (Not allowed to change the 'ConnectionString' property while the connection (state=Open).) that was already open.

The issue

When I call UseMySql, I get Cannot change the connection string on an open connection or "Not allowed to change the 'ConnectionString' property while the connection (state=Open).) that was already open.", depending on the connection type. This was working fine with Pomelo.EntityFrameworkCore.MySql 3.2.7, but is now an issue with 6.0.2.

Message: 
System.InvalidOperationException : Cannot change the connection string on an open connection.

Stack Trace: 
MySqlConnection.set_ConnectionString(String value) line 491
MySqlDbContextOptionsBuilderExtensions.UseMySql(DbContextOptionsBuilder optionsBuilder, DbConnection connection, ServerVersion serverVersion, Action`1 mySqlOptionsAction)
MySqlDbContextOptionsBuilderExtensions.UseMySql[TContext](DbContextOptionsBuilder`1 optionsBuilder, DbConnection connection, ServerVersion serverVersion, Action`1 mySqlOptionsAction)

Further technical details

MySQL version: 8 Operating system: Windows 10 Pomelo.EntityFrameworkCore.MySql version: 6.0.2

Coder3333 avatar Nov 01 '22 14:11 Coder3333

Have you tried using the SetConnectionString method on the DbContext to change the connection string?

mguinness avatar Nov 01 '22 15:11 mguinness

@mguinness - I do not want to change the connection string. I am merely calling UseMySql, which is throwing the exception. UseMySql should not be trying to change the connection string of the connection that I pass to it, as I have already established an open connection.

Coder3333 avatar Nov 01 '22 16:11 Coder3333

There's an old issue https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/169 that mentions AllowUserVariables=true;UseAffectedRows=false must be set otherwise the connection has to be closed. Will you be using the open MySqlConnection object outside EF Core?

mguinness avatar Nov 01 '22 21:11 mguinness

@mguinness - if I already have an open connection, I don't want to open another connection, especially if I am controlling a transaction with that original connection. If this library is simply not going to support very basic functionality like this, then it sounds like it is not really intended as a replacement for MySql.EntityFrameworkCore and cannot be trusted in production systems.

Coder3333 avatar Nov 02 '22 12:11 Coder3333

@mguinness - when I add the connection string options detailed in https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/169, things do work. I wish that the exception thrown gave some hints that this would resolve the issue.

Coder3333 avatar Nov 02 '22 12:11 Coder3333

Message: 
System.InvalidOperationException : Cannot change the connection string on an open connection.

That exception message is definitely unexpected. The one supposed to be thrown is:

https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/blob/147559fcc81ec51020b142d1a0eb6c42dde8c82f/src/EFCore.MySql/Extensions/MySqlDbContextOptionsBuilderExtensions.cs#L179

Looks like the inner exception type thrown by MySqlConnector was change from MySqlException to InvalidOperationException back in 2017 and we missed it.

lauxjpn avatar Nov 23 '22 10:11 lauxjpn

We keep this open until the backport is merged as well.

lauxjpn avatar Dec 14 '22 19:12 lauxjpn