tmsmith / Dapper-Extensions

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.
1.79k stars 586 forks source link

Insert Error #313

Open Kudiyarasu26 opened 1 year ago

Kudiyarasu26 commented 1 year ago

Object of type 'System.Int64' cannot be converted to type 'System.Int32 Show this type of the Error But Values Insert In Table

jchamberlain-pcty commented 1 year ago

I just ran into this myself. This comes from the library trying to update your DTO with the inserted identity. It assumes the identity is a long: https://github.com/tmsmith/Dapper-Extensions/blob/master/DapperExtensions/DapperImplementor.cs#L613

So when it tries to update your DTO to set the identity value, it throws that exception: https://github.com/tmsmith/Dapper-Extensions/blob/master/DapperExtensions/DapperImplementor.cs#L710

This means it successfully inserted the item (which is why you saw it in the table), but throws an exception processing the result.

I've seen on StackOverflow that the general advise seems to be to use a different key type in your table, such as a GUID or BIGINT.

To anyone thinking of submitting a PR for this, keep in mind that every SQL Dialect may need to be updated too. For instance, the SqlServerDialect casts the scope identity to a bigint: https://github.com/tmsmith/Dapper-Extensions/blob/master/DapperExtensions/Sql/SqlServerDialect.cs#L23