Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception InvalidOperationException with dapper and transaction #452

Closed
daikoz opened this issue Mar 14, 2018 · 5 comments
Closed

Exception InvalidOperationException with dapper and transaction #452

daikoz opened this issue Mar 14, 2018 · 5 comments

Comments

@daikoz
Copy link

daikoz commented Mar 14, 2018

Hi,

I try to execute a insert with dapper in a transaction:

            using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection("server=XXX;user id=Test;password=Test;database=Test"))
            {
                conn.Open();

                using (MySqlTransaction trans = conn.BeginTransaction())
                {
                    string test = "";
 
                    conn.Execute(@"INSERT INTO Test(@test)", new { test = test });
                    trans.Commit();
                }
            }

On execute, i have this exception:

System.InvalidOperationException
HResult=0x80131509
Message=The transaction associated with this command is not the connection's active transaction.
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 60
at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action2 paramReader) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2827 at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in C:\projects\dapper\Dapper\SqlMapper.cs:line 570 at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 443

Follow the test project:
TestMysqlconnector.zip

I use:

  • Visual studio 15.6.2
  • MySqlConnector 0.36.1
  • Dapper 1.50.4
  • .NET Core v2.1.0-preview1-26216.03

This code work fine with mysql connector of Oracle.

Regards

@bgrainger
Copy link
Member

bgrainger commented Mar 14, 2018

MySqlConnector currently requires the active transaction to be specified (to catch programming errors); this is a known breaking change from Oracle's Connector/NET. To do this with Dapper, write:

conn.Execute(@"INSERT INTO Test(@test)", new { test = test }, trans);

See this comment for the full explanation: #405 (comment)

@daikoz
Copy link
Author

daikoz commented Mar 18, 2018

Ok thanks. It is work fine with transaction parameter.

@bgrainger
Copy link
Member

bgrainger commented Apr 5, 2018

For future reference, you can also opt in to the Connector/NET behaviour by adding IgnoreCommandTransaction=true to your connection string (in 0.39.0 and later).

@daikoz
Copy link
Author

daikoz commented Apr 8, 2018

Is there a difference in performance?

@bgrainger
Copy link
Member

bgrainger commented Apr 8, 2018

No, there is no difference in performance when setting that option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants