0
我試圖在TransactionScope
裏面插入父子關係的數據,並且我得到了INSERT statement conflicted with the FOREIGN KEY constraint
錯誤。這裏是我的代碼:TransactionScope中的父子插入導致外鍵約束錯誤
using (var scope = new TransactionScope())
{
try
{
discount = discountDataAccess.Insert(discount);
switch (discount.Type)
{
case eDiscountType.PerCustomer:
InsertDiscountCustomer(discount.Id, idList);
break;
case eDiscountType.PerPackage:
InsertDiscountPackage(discount.Id, idList);
break;
}
scope.Complete();
return discount;
}
catch (Exception ex)
{
scope.Dispose();
throw;
}
}
當DiscountCustomer
或DiscountPackage
將被插入,Discount.Id
仍然是0
因爲沒有插入到數據庫中,直到scope.Complete()
數據調用。所以基本上我不能保存DiscountCustomer
或DiscountPackage
,直到我提交Discount
和Transaction
直到兩個都保存成功爲止未提交。
有沒有什麼辦法可以在TransactionScope
裏插入父母和子女?