How to return id from NHibernate ISession.SaveOrUpdate

纵然是瞬间 提交于 2019-12-23 10:27:34

问题


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


回答1:


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();
    }
}


来源:https://stackoverflow.com/questions/3110621/how-to-return-id-from-nhibernate-isession-saveorupdate

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