只有两种方法可以解决此问题。
1)不要使用 inverse="false"
<bag name="tableB" lazy="true" inverse="true" // instead of false batch-size="25" cascade="all-delete-orphan"> <key column="tabAId" /> <one-to-many /></bag>
此设置 (inverse =“ true”) 将指示NHibernate直接从数据库中删除 项目 。
虽然使用
inverse="false"will通常会导致:
- UPDATE(带null)==从集合中删除
- 删除项目==级联行为
2)使引用列可为空
这意味着,我们可以让NHibernate进行UPDATE和DELETE。因为column现在可以为空。
这些只是这里解决问题的两种方法。
我的偏好是: inverse =“ true”
为了正常工作, inverse="true"
我们总是必须在C#中分配关系的两端。这对于Add(),INSERT操作是必须的:
Parent parent = new Parent();Child child = new Child{ ... Parent = parent,};// unless initialized in the Parent type, we can do it hereparent.Children = parent.Children ?? new List<Child>();parent.Children.Add(child);// now just parent could be saved// and NHibernate will do all the cascade as expected// and because of inverse mapping - the most effective waysession.Save(parent);如我们所见,我们 已经 明确 指定
了关系的双方。必须从NHibernate逆映射中获利。这也是一个好习惯,因为稍后,当我们期望从数据库加载数据时,NHibernate将为我们设置该设置


![无法删除集合:[NHibernate.Exceptions.GenericADOException] 无法删除集合:[NHibernate.Exceptions.GenericADOException]](http://www.mshxw.com/aiimages/31/382907.png)
