DapperLib / DapperLib/Dapper

In transaction, @@IDENTITY return other table id, is it a bug?

Open
#1,632 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

db:mysql
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

My project use:
Dapper 1.50.2
Dapper.Contrib 2.0.0
DapperExtensions 1.6.3

and my code like this:

var sql = "insert into tb(name) select 'aaa' from dual where not exists(select 1 from tb where name='aaa')";
using(var connection = factory.CreateConnection())
using(var trans = connection.BeginTransaction())
{
    if(connection.Execute(sql, null, trans) > 0) 
    {
        return Convert.ToInt64(connection.ExecuteScalar("SELECT @@IDENTITY", null, trans));
    }
}
return 0;

while I published the code, I found "SELECT @@IDENTITY" returns other table's autoId, not tb's autoId?
Is my code wrong? how to fix it?

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 by identifying the database provider returned by factory.CreateConnection and reproduce the transaction using the SQL shown in the issue. Check whether the provider's @@IDENTITY behavior accounts for the returned value; done means establishing whether Dapper is involved and documenting a confirmed fix or limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.