Dapper.Contrib icon indicating copy to clipboard operation
Dapper.Contrib copied to clipboard

修复 Dapper.Contrib.Extensions 中 Insert 方法,此方法返回值为long类型,但内部各类型数据库获取自增ID,都是转换为int32,当ID超过超过Int32(2147483647)时,出现bug。

Open ypeuee opened this issue 3 years ago • 4 comments

修复 Dapper.Contrib.Extensions 中 Insert 方法,此方法返回值为long类型,但内部各类型数据库获取自增ID,都是转换为int32,当ID超过超过Int32(2147483647)时,出现bug。

主要涉及到的内容为接口 ISqlAdapter 中的方法:

` ///

/// The interface for all Dapper.Contrib database operations /// Implementing this is each provider's model. /// public partial interface ISqlAdapter { /// /// Inserts into the database, returning the Id of the row created. /// /// The connection to use. /// The transaction to use. /// The command timeout to use. /// The table to insert into. /// The columns to set with this insert. /// The parameters to set for this insert. /// The key columns in this table. /// The entity to insert. /// The Id of the row created. int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert);

.... `

,此方法返回值改为long可修复此问题。

ypeuee avatar Apr 07 '21 03:04 ypeuee

exactly, we want this update, especially when working with sqlite, it's id can be as big as int64.

benny856694 avatar Jul 19 '21 09:07 benny856694

this is an old issue for dapper.contrib, it's a breaking change so dapper.contrib won't merge the pr: https://github.com/DapperLib/Dapper/pull/1386

the work around is to insert a list with one item instead of single entity for this situation, the return value would be row count instead of primary key.

check the code of dapper contrib : https://github.com/DapperLib/Dapper.Contrib/blob/cf24f6bdc577b1e071c3764ddfb2cf3382531405/src/Dapper.Contrib/SqlMapperExtensions.cs#L380-L385

wswind avatar Jul 26 '21 07:07 wswind

@wswind How we get the value of the new PK then? It's not just that the method returns the PK value, it also assigns it to the Id property on the provided object.

mikesigs avatar Sep 02 '21 22:09 mikesigs

@mikesigs use dapper only, write the sql yourself. For sqlserver it's something like this: insert into ... output inserted.ID values ... , then you can assign the id yourself. This is just a workaround.

wswind avatar Sep 03 '21 01:09 wswind