title: How do I perform an insert and return inserted identity with Dapper?
body: <p>How do I perform an insert to database and return inserted identity with Dapper?</p>
  
  <p>I've tried something like this:</p>
  
  <pre><code>string sql = "DECLARE @ID int; " +
               "INSERT INTO [MyTable] ([Stuff]) VALUES (@Stuff); " +
               "SELECT @ID = SCOPE_IDENTITY()";
  
  var id = connection.Query&lt;int&gt;(sql, new { Stuff = mystuff}).First();
  </code></pre>
  
  <p>But it did't work.</p>
  
  <p>@Marc Gravell thanks, for reply.
  I've tried your solution but, still same exception trace is below</p>
  
  <pre><code>System.InvalidCastException: Specified cast is not valid
  
  at Dapper.SqlMapper.&lt;QueryInternal&gt;d__a`1.MoveNext() in (snip)\Dapper\SqlMapper.cs:line 610
  at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
  at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
  at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in (snip)\Dapper\SqlMapper.cs:line 538
  at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param) in (snip)\Dapper\SqlMapper.cs:line 456
  </code></pre>