How do I Insert or Update (or overwrite) a record using NHibernate?

后端 未结 6 1546
忘掉有多难
忘掉有多难 2021-02-02 00:51

I need to write a row to the database regardless of whether it already exists or not. Before using NHibernate this was done with a stored procedure. The procedure would attempt

6条回答
  •  无人共我
    2021-02-02 01:33

    Typically, NHibernate can rely on the unsaved-value to determine whether it should insert or create the entity. However, since you are assigning the ID, to NHibernate it looks like your entity has already been persisted. Therefore, you need to rely on versioning your object to let NHibernate know that it is a new object. See the following link for how to version your entity:

    http://web.archive.org/web/20090831032934/http://devlicio.us/blogs/mike_nichols/archive/2008/07/29/when-flushing-goes-bad-assigned-ids-in-nhibernate.aspx

提交回复
热议问题