Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ypeuee opened this issue Apr 7, 2021 · 4 comments

Comments

@ypeuee
Copy link

ypeuee commented Apr 7, 2021

修复 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 keyProperties, object entityToInsert);

....
`

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

@NickCraver NickCraver transferred this issue from DapperLib/Dapper May 8, 2021
@benny856694
Copy link

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

@wswind
Copy link
Contributor

wswind commented Jul 26, 2021

this is an old issue for dapper.contrib, it's a breaking change so dapper.contrib won't merge the pr:
DapperLib/Dapper#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 :

else
{
//insert list of entities
var cmd = $"insert into {name} ({sbColumnList}) values ({sbParameterList})";
returnVal = connection.Execute(cmd, entityToInsert, transaction, commandTimeout);
}

@mikesigs
Copy link

mikesigs commented Sep 2, 2021

@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.

@wswind
Copy link
Contributor

wswind commented Sep 3, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants