zzzprojects / Bulk-Operations

C# SQL Bulk Operations | High-performance C# bulk insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL, and SQLite.
https://bulk-operations.net
142 stars 36 forks source link

Retrieving IDs of inserted records #58

Closed AlwaysVbNet closed 4 years ago

AlwaysVbNet commented 4 years ago

After doing a bulk.BulkInsert(t) I would like to retrieve the IDs of the inserted records. At the moment I am able to get the RowsAffectedInserted only.

Any ideas?

JonathanMagnan commented 4 years ago

Hello @AlwaysVbNet ,

If you use the automapping, you need to set the AutoMapOutputIdentity = true: https://dotnetfiddle.net/ICZEez

If you explicitly set your mapping, you need to set the ColumnMappingDirectionType.Output: https://dotnetfiddle.net/Jw6x2c

The library will automatically populate your entity or datatable identity.

Let me know if that answers correctly to your question.

Best Regards,

Jon

AlwaysVbNet commented 4 years ago

AutoMapOutputIdentity = true worked!