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'.