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; \"
KB:2019779,"You may receive incorrect values when using SCOPE_IDENTITY() and @@IDENTITY", The OUTPUT clause is the safest mechanism:
string sql = @"
DECLARE @InsertedRows AS TABLE (Id int);
INSERT INTO [MyTable] ([Stuff]) OUTPUT Inserted.Id INTO @InsertedRows
VALUES (@Stuff);
SELECT Id FROM @InsertedRows";
var id = connection.Query(sql, new { Stuff = mystuff}).Single();