I have the following problem.
I have a parent class with a set of child objects.
public class Parent{ int _id; IList<Child> _childs = new List<Child>(); public IList<Child> Childs {get;} } public class Child{ int _id; string _name; Parent _parent; protected Child(){} public Child(Parent parent, string name){ _parent = parent; _name = name; } }
Classes map to nhibernate for the database, where the tblChild.colName column has a unique index.
// Parent <bag name="_childs" access="field" cascade="all-delete-orphan" inverse="true"> <key column="ParentId" /> <one-to-many class="Parent" /> </bag> // Child <many-to-one name="_parent" column="ParentId" cascade="none" access="field">
My problem: The following code throws an exception due to a unique index:
Parent parent = new Parent(); Child child1 = new Child(parent, "Child1"); Child child2 = new Child(parent, "Child2"); Child child3 = new Child(parent, "Child3"); parent.Childs.Add(child1); parent.Childs.Add(child2); parent.Childs.Add(child3); parentDao.Save(parent); parentDao.FlushAndClear(); Child child4 = new Child(parent, "Child1");
The reason for the exception is that NHibernate first inserts child4, and then removes child1. Why does NHibernate do this? Can someone explain and can help me solve this problem?
source share