Why does NHibernate not realise my domain object needs updating, not inserting?
I have a web page which uses NHibernate to load a domain object. The object's state is then stored in the page controls, and when the user clicks the save button, a new object is created and its properties (included the Id) are populated from the page controls. I then call session.Save() on the object.
This to me means that NHibe开发者_如何学Gornate should use an UPDATE rather than an INSERT, because the Id property has been set and differs from the unssaved-value. However, NHibernate is attempting to insert it.
I have included the part of the mapping file relating to the Id below:
<id name="Id" column="StoredWillId" unsaved-value="0">
<generator class="native" />
</id>
Can anyone explain what's going on here?
Thanks
David
Call session.SaveOrUpdate(). session.Save() inserts, session.Update() updates, session.SaveOrUpdate() saves if Id is 0, otherwise updates.
精彩评论