I want to save a single object to the database twice. I have this code:
using (var ss = NHibHelp.OpenSession())
using (var tt = ss.BeginTransaction())
{
var entity = new Entity();
ss.Save(entity);
ss.Save(entity);
tt.Commit();
}
But this results in only one row being added to the database. How do I insert a single object into the database twice (with two different Ids) ?
You shouldn’t do this – NHibernate maintains “object identity” within it’s session, so it will not differentiate between ..well.. the same object. I would really advise against this, and a better solution would be to look at a way of cloning the object (either via reflection, or a Clone method), and then saving the cloned object.
If you want to ignore my advice above, you can get it to work by evicting the entity from the session, setting it’s id back to it’s unsaved value (depends on your mapping, but probably 0), and then saving it again.
It might also work if you just called session.Merge(entity) twice (you probably have to reset the id to it’s unsaved value after the first call).
Alternatively you could use a stateless session with session.Merge() and then you don’t have to evict the entity between Save’s.