Bug #116735 Must use 'MySql.Data.MySqlClient.MySqlConnection' with .Net Core Entities
Submitted: 20 Nov 2024 18:28 Modified: 21 Nov 2024 10:16
Reporter: Eric Finch Email Updates:
Status: Analyzing Impact on me:
None 
Category:Connector / NET Severity:S4 (Feature request)
Version:9.1.0 OS:Any
Assigned to: Omar Chavez CPU Architecture:Any
Tags: MySql.Data.MySqlClient.MySqlConnection

[20 Nov 2024 18:28] Eric Finch
Description:
The MySQLRelationalConnection class hides the base class' DbConnection property and changes the type from the generic DbConnection to MySql.Data.MySqlClient.MySqlConnection.  Typically, EF implementations allow the generic DbConnection, so different connection types can be used.  In my case I am trying to use 'MySqlConnector.MySqlConnection', but cannot due to this code limitation.

  Message: 
System.InvalidCastException : Unable to cast object of type 'MySqlConnector.MySqlConnection' to type 'MySql.Data.MySqlClient.MySqlConnection'.

  Stack Trace: 
MySQLRelationalConnection.get_DbConnection()
MySQLRelationalConnection.Open(Boolean errorsExpected)
RelationalConnection.BeginTransaction(IsolationLevel isolationLevel)
RelationalConnection.BeginTransaction()
BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
RelationalDatabase.SaveChanges(IList`1 entries)
StateManager.SaveChanges(IList`1 entriesToSave)
StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
<>c.<SaveChanges>b__112_0(DbContext _, ValueTuple`2 t)
MySQLExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
DbContext.SaveChanges()

How to repeat:
Instantiate the DbContext with a connection of type 'MySqlConnector.MySqlConnection', instead of 'MySql.Data.MySqlClient.MySqlConnection' and call SaveChanges.

Suggested fix:
Update MySQLRelationalConnection and related code to treat the DbConnection property like a generic DbConnection , instead of forcing it to be a 'MySql.Data.MySqlClient.MySqlConnection'.