Someone on our data team added a database constraint and, while it's perfectly valid and desirable, it creates great problems for NHibernate because there doesn't seem to be a way to override NHibernate's save order.
Given a (silly example) class like this:
public Person
{
public virtual string FirstName { get; set; }
public virtual bool IsCurrent { get; set; }
}
and a constraint that only one record in the backing table can be "IsCurrent=true" at the same time . . .
If I try to "deprecate" an existing record by setting IsCurrent=false, and replace it with a new record with IsCurrent=true, I get an ADO exception on Save because NHibernate tries to perform the Insert first, violating the SQL Server constraint that only one record can be "IsCurrent=true" at once.
I see two options:
1) Can SQL Server be configured to check constraints only at the end of a transaction? The following statement (the "update" of the old row to "IsCurrent=false" would un-break the constraint.
2) Can NHibernate's save order (for instances of the same type) be modified or "hinted" in any way?
Thanks! Jeff