【发布时间】:2015-06-26 16:18:46
【问题描述】:
我正在尝试在TransactionScope 中插入父子关系的数据,但出现INSERT statement conflicted with the FOREIGN KEY constraint 错误。这是我的代码:
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 中同时插入父子元素?
【问题讨论】:
标签: c# sql-server ado.net transactionscope