How to return id from NHibernate ISession.SaveOrUpdate

I use ISession.SaveOrUpdate to insert new objects and update an existing one.

If I use ISession.Save (..), this returns the ID of the inserted record.

For SaveOrUpdate, I do the following:

   public int Save(Vehicle entity) {
        using (var txn = _session.BeginTransaction()) {
            _session.SaveOrUpdate(entity);
            txn.Commit();
        }
        return entity.Id;
    }

Is this the best way to get my identity back?

Thank,

Ben

+5
source share
1 answer

There is nothing wrong. But since the identifier is set on the object, why not make the return void method:

   public void Save(Vehicle entity) {
    using (var txn = _session.BeginTransaction()) {
        _session.SaveOrUpdate(entity);
        txn.Commit();
    }
}
+7
source

All Articles