Is Audit the only way of getting inserted Id's?
Closed this issue · 4 comments
when using
await this.database.BulkInsert(newItems);
is there any other way of getting the items with the updated ids? Or having newItems
have the database generated Keys set on it without having to create a new list and passing it to bulk options audit entries?
Hello @cdbridger-tracplus ,
It depends on what exactly you want. People usually use one of the two following mapping:
.Identity(x => x.MyID)
Output(x => x.MyID)
In both cases, your entities will be populated by the identity generated on the database side. You can retrieve the identity value by just looking at your entities instead of using the audit feature.
You can learn more from Fluent Mapping and Identity Propagation in those articles
Let me know if that answers your question.
Best Regards,
Jon
I can't seem to get it to work
As you can see in the above image, the entities passed in don't have their Id's set after calling BulkInsert (they are still null, although the database has generated the id values for them in the database)
Here is the model:
Here is my DapperPLusManager Setup
I'm assuming I'm doing something fundamentally wrong if the entities are supposed to have their Database Id's populated on them (in memory) after I insert them but I can't tell what the error is from reading the docs.
(This is why after I insert I'm currently doing a get all after insert which is obviously bonkers).
Any help would be much appreciated!
Cheers
EDIT: I also thought it may be because of the nullable id and changed it to non-nullable, but the id just stays as the value 0 after bulk insert
Hello @cdbridger-tracplus ,
First, you don't need to specify the Output
mapping (you probably did it because you tried everything hehe)
I have a big hypothesis about why you don't see the IDs. You are currently passing an IEnumerable
of markers and not a list or an array. So what probably happens is every time you iterate over this IEnumerable
, you recreate the list from scratch. For example, if the list comes by reading a file, you read that file every time a method like ToList
is called to this IEnumerable
.
If you transform this IEnumerable
to a list first, you will see the Ids
populated correctly.
Let me know if that cause and solution were clear enough.
Best Regards,
Jon
Ah that was the trick! Thank you very much.