|
1 | 1 | ---
|
2 |
| -lastmod: 2019-07-30 |
| 2 | +lastmod: 2021-09-21 |
3 | 3 | date: 2018-09-29
|
4 | 4 | title: Transaction Usage
|
5 | 5 | customtitle: "Fix: The transaction associated with this command is not the connection’s active transaction"
|
@@ -30,42 +30,38 @@ To easily migrate code from Connector/NET, use the `IgnoreCommandTransaction=tru
|
30 | 30 | ### ADO.NET example
|
31 | 31 |
|
32 | 32 | ```csharp
|
33 |
| -using (var connection = new MySqlConnection(...)) |
34 |
| -{ |
35 |
| - connection.Open(); |
36 |
| - using (var transaction = connection.BeginTransaction()) |
37 |
| - using (var command = connection.CreateCommand()) |
38 |
| - { |
39 |
| - command.CommandText = "SELECT ..."; |
40 |
| - |
41 |
| - // *** ADD THIS LINE *** |
42 |
| - command.Transaction = transaction; |
43 |
| - |
44 |
| - // otherwise, this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction. |
45 |
| - command.ExecuteScalar(); |
46 |
| - |
47 |
| - transaction.Commit(); |
48 |
| - } |
49 |
| -} |
| 33 | +using var connection = new MySqlConnection(...); |
| 34 | +connection.Open(); |
| 35 | + |
| 36 | +using var transaction = connection.BeginTransaction(); |
| 37 | +using var command = connection.CreateCommand(); |
| 38 | +command.CommandText = "SELECT ..."; |
| 39 | + |
| 40 | +// *** ADD THIS LINE *** |
| 41 | +command.Transaction = transaction; |
| 42 | + |
| 43 | +// otherwise, this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction. |
| 44 | +command.ExecuteScalar(); |
| 45 | + |
| 46 | +// ... remaining code |
| 47 | +transaction.Commit(); |
50 | 48 | ```
|
51 | 49 |
|
52 | 50 | ### Dapper Example
|
53 | 51 |
|
54 | 52 | ```csharp
|
55 |
| -using (var connection = new MySqlConnection(...)) |
56 |
| -{ |
57 |
| - connection.Open(); |
58 |
| - using (var transaction = connection.BeginTransaction()) |
59 |
| - { |
60 |
| - // this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction. |
61 |
| - connection.Query("SELECT ..."); |
62 |
| - |
63 |
| - // use this instead: |
64 |
| - connection.Query("SELECT ...", transaction: transaction); |
65 |
| - |
66 |
| - transaction.Commit(); |
67 |
| - } |
68 |
| -} |
| 53 | +using var connection = new MySqlConnection(...); |
| 54 | +connection.Open(); |
| 55 | +using var transaction = connection.BeginTransaction(); |
| 56 | + |
| 57 | +// this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction. |
| 58 | +connection.Query("SELECT ..."); |
| 59 | + |
| 60 | +// use this instead: |
| 61 | +connection.Query("SELECT ...", transaction: transaction); |
| 62 | + |
| 63 | +// ... remaining code |
| 64 | +transaction.Commit(); |
69 | 65 | ```
|
70 | 66 |
|
71 | 67 | ## Further Reading
|
|
0 commit comments