我有一个接受发票的Web服务,其中包含lineitem子项。然后它更新数据库,使用nhibernate创建或更新发票。
当更新发票时,它将与它现在拥有的所有行项目子项一起传递给Web服务。添加和更新工作正常。但是,如果Web服务使用者从以前保留的发票中删除子行项目并重新提交,则该行项目实际上不会从数据库中删除,而是将其对父行的返回引用设置为空。我正在使用(尝试使用)cascade=“all delete orphan”但没有成功。
我怀疑问题可能是由于操作的无状态性质造成的(我没有首先在Web服务端的invoice.lineitem list中包含lineitem,然后将其删除,而是按照现在的情况获取lineitem的列表)。但是,nhibernate足够聪明,可以让后面的引用列为空,所以我希望有一种直接的方法可以让它删除该行。
以下是映射(简化)。
Parent object (Invoice):
<property name="InvoiceNumber" />
<!-- If inverse="true", InvoiceId is NOT set to NULL and the record remains -->
<bag name="LineItemList" table="lineitems" inverse="false" cascade="all-delete-orphan">
<key column="InvoiceId"/>
<one-to-many
class="LineItem"/>
</bag>
子对象(LineItems):
<many-to-one lazy="false" name="Parent" column="InvoiceID" not-null="false"
class="Invoice,Company.Business"
/>
<property name="LineItemNumber" />
<property name="SalesAmount"/>
Web服务持久性代码如下所示:
[WebMethod]
公共发票PutInvoice(发票发票)
{
//需要重新生成父引用,请参见
Blog
foreach (LineItem item in invoice.LineItems)
{
item.Parent = invoice;
}
using (PersistenceManager pm = new PersistenceManager())
{
pm.Save<Invoice>(invoice);
}
return invoice; // Return version potentially modified with DB-assigned ID
}