使用dapper.net返回mysql LAST_INSERT_ID()时,转换无效 [英] Invalid cast when returning mysql LAST_INSERT_ID() using dapper.net

查看:528
本文介绍了使用dapper.net返回mysql LAST_INSERT_ID()时,转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处已为MSSQL涵盖了此问题:

This question has been covered for MSSQL here:

如何执行插入并使用Dapper返回插入的身份?

但是此解决方案不适用于mysql.

but this solution does not work with mysql.

要使用MySQL将LAST_INSERT_ID()转换为整数,您必须执行以下操作:

To cast the LAST_INSERT_ID() to integer with mysql you have to do this:

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中解决了这个问题吗?

Has anyone resolved this issue in MySQL?

我已经通过以下方法使之起作用:

I've managed to make this work with the following:

var id = connection.Query<ulong>("SELECT CAST(LAST_INSERT_ID() AS UNSIGNED INTEGER);").Single();

也许不理想,但是可以.

Perhaps not ideal, but it works.

推荐答案

在这里,我将其保留为所有可能对此问题进行搜索的人的答案.

I'll leave this here as an answer for anyone else who might search on this problem.

我已经通过以下方法使之起作用:

I've managed to make this work with the following:

var id = connection.Query<ulong>("SELECT CAST(LAST_INSERT_ID() AS UNSIGNED INTEGER);").Single();

也许不理想,但是可以.

Perhaps not ideal, but it works.

这篇关于使用dapper.net返回mysql LAST_INSERT_ID()时,转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆