开发者

How to save and then update same class instance during one request with NHibernate?

I'm relatively new to NHibernate and I've got a question about it. I use this code snippet in my MVC project in Controller's method:

MyClass entity = new MyClass 
                    {
                        Foo = "bar"
                    };

_myRepository.Save(entity);

....

entity.Foo = "bar2";
_myRepository.Save(entity);

The first time entity saved in database succesfully. But the second time not a single request doesnt go to database. My method save in repository just does:

public void Save(T entity)
{
  _session.SaveOrUpdate(entity);
}

What should I do to be able to save and then update this entity during one request? If I a开发者_开发百科dd _session.Flush(); after saving entity to database it works, but I'm not sure, if it's the right thing to do.

Thanks


This is the expected behavior.

  • Changes are only saved on Flush
  • Flush may be called explicitly or implicitly (see 9.6. Flush)
  • When using an identity generator (not recommended), inserts are sent immediately, because that's the only way to return the ID.


you should be using transactions.
a couple of good sources: here and here.
also, summer of nHibernate is how I first started with nHibernate. it's a very good resource for learning the basics.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜