DapperLib / DapperLib/Dapper.Contrib

MySQL Insert does not return inserted ID if you do not open the connection first

Open
#64 1 comment 1 reaction 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.