ORMLite OpenTransaction batch Insert & Getting back new Guids

旧时模样 提交于 2019-12-13 02:15:57

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21201291/ormlite-opentransaction-batch-insert-getting-back-new-guids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!