How do I perform an insert and return inserted identity with Dapper?

后端 未结 8 906
忘掉有多难
忘掉有多难 2020-11-28 01:45

How do I perform an insert to database and return inserted identity with Dapper?

I\'ve tried something like this:

string sql = \"DECLARE @ID int; \"          


        
8条回答
  •  广开言路
    2020-11-28 02:04

    There is a great library to make your life easier Dapper.Contrib.Extensions. After including this you can just write:

    public int Add(Transaction transaction)
    {
            using (IDbConnection db = Connection)
            {
                    return (int)db.Insert(transaction);
            }
    }
    

提交回复
热议问题