Question

I am trying to use ServiceStack ORMLite to run this:

        using (var o = Conn.OpenDbConnection()) {
            using (var t = o.OpenTransaction()) {
              foreach(var item in items) o.Insert(item);
              t.Commit();
              //now, how do I get back the new item ids I have just inserted?
            }
        }

In the code, how do I get back the batch new Ids? Also noticed the non-batch version GetLastInsertId() only return a Long. What do I do when my Id type is a GUID? Thank you.

Also, while you are here, I would like to also ask, if t.Commit(); fails an exception is thrown, is there a need there to call t.Rollback();? since the transaction is over anyway?

Was it helpful?

Solution

In v4, db.Save() automatically populates the AutoIncrement Id, e.g:

db.Save(item);
item.Id //populated with the auto-incremented id

Otherwise you can select the last insert id using:

var itemId = db.Insert(item, selectIdentity:true);

Here are more examples showcasing OrmLite's new API's.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top