如何从NHibernate ISession.SaveOrUpdate返回ID [英] How to return id from NHibernate ISession.SaveOrUpdate

查看:264
本文介绍了如何从NHibernate ISession.SaveOrUpdate返回ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ISession.SaveOrUpdate插入新对象并更新现有对象.

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

如果我使用ISession.Save(..),则返回插入记录的身份.

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

对于SaveOrUpdate,我正在执行以下操作:

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?

谢谢

推荐答案

这没什么不对的.但是由于身份是在对象上设置的,为什么不使该方法返回void:

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

这篇关于如何从NHibernate ISession.SaveOrUpdate返回ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆