How does NHibernate determine whether to insert or update a record?

When using Session.SaveOrUpdate(myEntity); How does NHibernate decide how to insert a new record or update an existing one?

I'm having trouble saving one object in an S # arp project. It is retrieved from the repository, then stored in session state for several web requests, and then saved back to the database with one property changed (not S # arp [DomainSignature] ).

At runtime, I compared the object that should be saved with the recently obtained version directly from the database using the Equals() method and returns true. However, the object still finishes creating a new row in the database.

Elsewhere in the application, this works fine, but I am hoping for a pointer to how NHib works on this.

+4
source share
1 answer

Basically SaveOrUpdate() looking for an identifier. If the identifier is present, it will update the record in the database. If the identifier is missing, it will create a new entry.

However, it looks like you might have something scared in your session. You might want to try SaveOrUpdateCopy() to solve this problem.

+2
source

All Articles