Ali Bahrami
Ali Bahrami

Reputation: 6073

Transfer an object from one NHibernate session two another session

Two NHibernate sessions are already created, I create a new model object and save it with an Id in session1, then I use Merge method from session2 to commit its changes.

// session1, NHibernate
var obj1 = new FooModel();
session1.Save(obj1, Guid.NewId());

// session2, NHibernte
session2.Merge(obj1);
session2.Commit();

// and finally
session1.Commit();

and the result I think is kind of unexpected! by running code above NHibernate will insert 2 records with different Id's.

Upvotes: 0

Views: 645

Answers (1)

David Osborne
David Osborne

Reputation: 6791

I wonder if this is because the object in session2 is transient when it's added? It shouldn't be because you're allocating the key in the call to Save(), however the behaviour you're observing fits with that situation.

If you change the code to:

// session1, NHibernate
var obj1 = new FooModel();

session1.Save(obj1, Guid.NewId());
session1.Commit();

// session2, NHibernte
session2.Merge(obj1);
session2.Commit();

What's the effect?

Upvotes: 2

Related Questions