DapperLib / DapperLib/Dapper.Contrib
MySQL Insert does not return inserted ID if you do not open the connection first
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 293
- Forks
- 109
- PR merge metrics
- No merged PRs in 30d
Description
The MySql Adapter in https://github.com/StackExchange/Dapper/blob/master/Dapper.Contrib/SqlMapperExtensions.Async.cs does not check for an open connection. Consequently, if a closed connection is used, the following two commands (lines 403 & 404) are issued on separate connections. This means that the "SELECT LAST_INSERT_ID()" command will return 0.
await connection.ExecuteAsync(cmd, entityToInsert, transaction, commandTimeout).ConfigureAwait(false);
var r = await connection.QueryAsync<dynamic>("SELECT LAST_INSERT_ID() id", transaction: transaction, commandTimeout: commandTimeout).ConfigureAwait(false);
One could fix the issue by either checking the connection is open and opening it if it isnt. Or maybe use QueryAsync and concatenate the insert query and the select query into one call.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in Dapper.Contrib/SqlMapperExtensions.Async.cs around lines 403-404 and reproduce the insert using a closed MySQL connection. Trace how ExecuteAsync and QueryAsync use the connection, then verify that the inserted ID is returned correctly without requiring the caller to open the connection first.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100