NHibernate Unique Constraint on Name and Parent Object fails because NH inserts Null

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2010-04-22T08:06:15Z Indexed on 2010/04/22 11:23 UTC
Read the original article Hit count: 368

Hi,

I have an object as follows

Public Class Bin

  Public Property Id As Integer

  Public Property Name As String

  Public Property Store As Store

End Class

Public Class Store

    Public Property Id As Integer

    Public Property Bins As IEnumerable(Of Bin)

End Class

I have a unique constraint in the database on Bin.Name and BinStoreID to ensure unique names within stores. However, when NHibernate persists the store, it first inserts the Bin records with a null StoreID before performing an update later to set the correct StoreID. This violates the Unique Key If I persist two stores with a Bin of the same name because The Name columns are the same and the StoreID is null for both.

Is there something I can add to the mapping to ensure that the correct StoreID is included in the INSERT rather than performing an update later? We are using HiLo identity generation so we are not relying on DB generated identity columns

Thanks

James

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping