Bug #116735 Must use 'MySql.Data.MySqlClient.MySqlConnection' with .Net Core Entities

Submitted: 20 Nov 2024 18:28 Modified: 30 Sep 12:29
Reporter: Eric Finch Email Updates:
Status: Won't fix Impact on me:
None 
Category:Connector / NET Severity:S4 (Feature request)
Version:9.1.0 OS:Any
Assigned to: Jose Ramirez Ruiz 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'.
[29 Sep 23:22] Jose Ramirez Ruiz
Hello, 

This provider is designed to operate with Connector/NET (MySql.Data.MySqlClient.MySqlConnection). MySqlConnector.MySqlConnection belongs to a different ADO.NET provider and is not supported or interchangeable with Connector/NET types.

Supporting another provider’s connection implementation is outside this project’s scope.

Regards,
MySQL Team
[30 Sep 12:29] Eric Finch
Would there be a downside to treating the internal dbconnection like a generic DbConnection, instead of forcing it to a specialized type?  I did not notice any place where you use any specialized interfaces.  It would simply be nice if the library offered more options to consumers, especially if it didn't have a downside.
[30 Sep 20:11] Jose Ramirez Ruiz
Hi Eric,

Thank you for the follow-up. Although technically possible, supporting the generic DbConnection and/or MySqlConnector.MySqlConnection would require broader changes and a dedicated compatibility/test effort across Connector/NET-specific connection, pooling, cloning, and error-handling behavior. Given the changes it implies, this is currently out of scope for the foreseeable future.

Regards,
MySQL Team

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.052 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.