简体   繁体   English

使用dapper.net返回mysql LAST_INSERT_ID()时无效的强制转换

[英]Invalid cast when returning mysql LAST_INSERT_ID() using dapper.net

This question has been covered for MSSQL here: MSSQL在这里已经涵盖了这个问题:

How do I perform an insert and return inserted identity with Dapper? 如何使用Dapper执行插入并返回插入的标识?

but this solution does not work with mysql. 但是这个解决方案不适用于mysql。

To cast the LAST_INSERT_ID() to integer with mysql you have to do this: 要使用mysql将LAST_INSERT_ID()为整数,您必须执行以下操作:

SELECT CAST(LAST_INSERT_ID() AS UNSIGNED INTEGER);

The stack trace is: 堆栈跟踪是:

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

Has anyone resolved this issue in MySQL? 有没有人在MySQL中解决这个问题?

EDIT: 编辑:

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. 也许不理想,但它的确有效。

I can actually shed some additional light on this because I just spent the last hour wondering why my SELECT LAST_INSERT_ID() query worked on one MySQL server but not another. 我实际上可以对此有所了解,因为我只花了最后一小时想知道为什么我的SELECT LAST_INSERT_ID()查询在一个MySQL服务器上工作而在另一个MySQL服务器上工作。 One server is running MySQL 5.5.11 (production) and the other 5.5.31 (local dev). 一台服务器运行MySQL 5.5.11(生产),另一台运行5.5.31(本地开发)。

Prior to versions 5.1.67, 5.5.29 and 5.6.9 (in each respective release) LAST_INSERT_ID() used to return a signed integer. 在版本5.1.67,5.5.29和5.6.9(在每个相应版本中)之前, LAST_INSERT_ID()用于返回有符号整数。

Now LAST_INSERT_ID() returns an unsigned BIGINT which meant this code which worked on my 5.5.31 server worked: 现在LAST_INSERT_ID()返回一个无符号的BIGINT ,这意味着在我的5.5.31服务器上运行的代码工作:

var id = cn.Query<ulong>("SELECT LAST_INSERT_ID();").First();

...but is broken when executed against the older 5.5.11 server. ...但是对旧的5.5.11服务器执行时会损坏。

It's documented here: 它在这里记录:

http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id

The value has a type of BIGINT UNSIGNED as of MySQL 5.5.29, BIGINT (signed) before that. 该值的类型为BIGINT UNSIGNED ,自MySQL 5.5.29起,之前为BIGINT (签名)。

My initial solution was to cast the result of LAST_INSERT_ID() to an unsigned BIGINT to make the code portable across both these server versions but (surprise, surprise) the MySQL team added a roadblock. 我最初的解决方案是将LAST_INSERT_ID()的结果转换为未签名的BIGINT以使代码可以在这两个服务器版本中移植,但是(惊讶,惊讶)MySQL团队添加了一个障碍。

You can't cast LAST_INSERT_ID() directly to an unsigned (or even signed) BIGINT using the CAST() function because it's not supported. 您不能使用CAST()函数LAST_INSERT_ID()直接转换为无符号(甚至已签名)的BIGINT ,因为它不受支持。 The only integer types you can cast to are SIGNED INTEGER and UNSIGNED INTEGER . 您可以强制转换为SIGNED INTEGERUNSIGNED INTEGER的唯一整数类型。 This is a pain because if for whatever reason you really need an auto incrementing BIGINT id which increments past 4294967295 then an unsigned integer won't be a large enough type to cast to. 这是一种痛苦,因为如果由于某种原因你真的需要一个自动递增的BIGINT id,它增加超过4294967295那么无符号整数将不会是一个足够大的类型来强制转换。

使用Convert.ToInt64(value)为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM