Pomelo.EntityFrameworkCore.MySql
Pomelo.EntityFrameworkCore.MySql copied to clipboard
`Cannot change the connection string on an open connection` from UseMySql
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
Have you tried using the SetConnectionString method on the DbContext to change the connection string?
@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.
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 - 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.
@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.
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.
We keep this open until the backport is merged as well.