Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return id from NHibernate ISession.SaveOrUpdate

Tags:

nhibernate

I am using ISession.SaveOrUpdate to insert new objects and updaet existing.

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

For SaveOrUpdate I am doing 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 return my identity?

Thanks,

Ben

like image 750
Ben Foster Avatar asked Jun 24 '10 13:06

Ben Foster


1 Answers

There's nothing wrong with that. But since the identity is set on the object, why not make the method return void:

   public void Save(Vehicle entity) {
    using (var txn = _session.BeginTransaction()) {
        _session.SaveOrUpdate(entity);
        txn.Commit();
    }
}
like image 155
Jamie Ide Avatar answered Sep 29 '22 09:09

Jamie Ide