RepoDB
                                
                                 RepoDB copied to clipboard
                                
                                    RepoDB copied to clipboard
                            
                            
                            
                        Bug: Calling BeginTransaction on a MySQL Connection fails
Following the sample code on the website, if you don't open a MySql connection before calling BeginTransaction, you get the following error.
System.InvalidOperationException : The connection is not open.
   at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
   at MySql.Data.MySqlClient.MySqlConnection.BeginTransaction(IsolationLevel iso, String scope)
   at MySql.Data.MySqlClient.MySqlConnection.BeginTransaction()
the code I actually used is as follows:
using (var connection = new MySqlConnection(AppSettings.ConnectionString)) {
                //connection.Open();
                using (var transaction = connection.BeginTransaction()) {
                    // Call the method
                    var CompanyId = connection.Insert<Company, int>(CompanyInfo, transaction: transaction);
                    CompanyInfo.CompanyId = CompanyId;
                    UserInfo.HashPassword();
                    UserInfo.UpdateCompanyId(CompanyId);
                    var UserAccount = connection.Insert<UserAccount, int>(UserInfo, transaction: transaction);
                    // Commit
                    transaction.Commit();
                }
                //connection.Close();
            }
if I uncomment the connection.Open() and connection.Close() it run's just fine. Otherwise, I get the error above.
Also, other mysql queries work just fine without having to open the connection.
Like this works just fine:
using var connection = AppSettings.MysqlConnection(); //this is just a method I wrote to return a new connection, instead of doing what I did above. that was for verification that my method wasn't doing anything crazy
            return connection.Query<UserAccount>(aUser => aUser.Username == aUsername).First();
I did see this in the unit tests that I'll probably incorporate to my code. I saw the EnsureOpen() call
 using (var transaction = connection.EnsureOpen().BeginTransaction())
                {
                    // Act
                    connection.BatchQuery<CompleteTable>(0, 10, OrderField.Parse(new { Id = Order.Ascending }), it => it.Id != 0, transaction: transaction);
                }
Thanks for filing this, there is no problem on this in relation to the process you made. We will update the documentation to add the calls to either Open() or EnsureOpen().
You are absolutely correct that ADO.Net requires an open connection to start a transaction (a requirement for all MVCC based database client drivers), therefore, you need to have an explicit call there (either the Open() or the EnsureOpen() method) first.
Please be reminded, calling an EnsureOpen() returns the instance of IDbConnecetion instead of MySqlConnection (atleast in your case), that is on purpose and please be aware of this.
We will not do any action on this besides updating the documentation, or please do let us know if you have any other concerns.
The fix has been applied to the documentation side. Can be found here.