这里的 MSSQL 已经涵盖了这个问题:
How do I perform an insert and return inserted identity with Dapper?
但此解决方案不适用于 mysql。
要使用 mysql 将 LAST_INSERT_ID()
转换为整数,您必须这样做:
SELECT CAST(LAST_INSERT_ID() AS UNSIGNED INTEGER);
堆栈跟踪是:
Dapper.<QueryInternal>d__13`1.MoveNext() in sqlMapper.cs:607
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +159
System.Linq.Enumerable.ToList(IEnumerable`1 source) +36
Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in sqlMapper.cs:535
有人在 MySQL 中解决了这个问题吗?
编辑:
我已经设法通过以下方式完成了这项工作:
var id = connection.Query<ulong>("SELECT CAST(LAST_INSERT_ID() AS UNSIGNED INTEGER);").Single();
也许不理想,但它有效。
最佳答案
我将把这个留在这里,作为其他任何可能搜索此问题的人的答案。
我已经设法通过以下方式完成了这项工作:
var id = connection.Query<ulong>("SELECT CAST(LAST_INSERT_ID() AS UNSIGNED INTEGER);").Single();
也许不理想,但它有效。
关于c# - 使用 dapper.net 返回 mysql LAST_INSERT_ID() 时强制转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8477398/