var customerId = db.Insert(customer, selectIdentity: true); //Get Auto Inserted Id
what if i set selectIdentity false?I can not find any documents.
var customerId = db.Insert(customer, selectIdentity: true); //Get Auto Inserted Id
what if i set selectIdentity false?I can not find any documents.
selectIdentity: true
runs an additional query that returns the auto-incremented id. selectIdentity: false
is the default which doesn’t execute the extra query or return the auto-incremented id.
This option is only relevant for [AutoIncrement]
primary keys.
If I’m not mistaken, I think in the Dapper approach when selectIdentity was true, it wouldn’t just query the new Id, but would also inject it to the newly inserted object … saving that extra step if you need to do more work with the object reference or return it to a caller.
I’ve had to do something like this a few times:
thing.Id = (int)Db.Insert(thing, true);
return thing;
That’s what the higher-level db.Save() API does for you automatically, e.g:
db.Save(thing);
thing.Id //= populated AutoIncremented Id
Hadn’t noticed, good to know. If I know I’m inserting a new object, I prefer Insert to use only 1 query instead of 2 for save … but now I see save (when used for insert) is actually 3 calls.
db.Save()
only adds 1 extra call to check for the Id. The Insert and Get Id is 2 SQL Statements but only using 1 DB call.