NHibernate missing or invalid object exceptions, with two databases
Currently, I’m working on a project which involves working with SqLite databases, and one of them is reserved for Undo operations, that is to say, the current state of an object in the main database is persisted to the Undo database file so that it can then be retrieved later on. The reason we are using two distinct files is, SqLite is not that stable, so if a transaction fails, or somehow one file is corrupted, the other is not affected.
This leads to problems within the NHibernate persistence layer, as NHibernate has difficulty in telling one object from another across multiple sessions, so when you persist the Undo operation, you get errors when you try to persist the Edit operation. Similarly, when trying to validate a new object, NHibernate gets confused because it doesn’t know how to instantiate the child object collections.
The solution: NHibernate’s Session.Merge method.
Merge updates the session with the values from the disconnected instance, allowing NHibernate to process the object.
[PH for more detail]