Skip to content

Using int type as identity key results in an error (library always try to convert the value to long) #278

@spetz

Description

@spetz

Hey,

I've found out the following issue when using int as a key and then trying to insert the record into the Postgres table (which is configured to be of identity type - so it's automatically incremented value by DB).

System.ArgumentException: Object of type 'System.Int64' cannot be converted to type 'System.Int32'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)

This is the sample entity:

public class MyType
{
    public int Id { get; set; }
}

and a mapping which doesn't really do anything

internal sealed class MyTypeMapping : ClassMapper<MyType>
{
    public MyTypeMapping()
    {
        Map(x => x.Id).Type(DbType.Int32).Key(KeyType.Identity);
        AutoMap();
    }
}

Seems that it all boils down to the following line - which always does assume that the ID will be of long type, even though it is int which is a valid type for the column (e.g. smallint, integer, biginteger).

result = connection.Query<long>(sql, dynamicParameters, transaction, false, commandTimeout, CommandType.Text);

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions