我有这样的代码:
using (TransactionScope transactionScope = new TransactionScope())
{
SetDefaults(products);
// Map the SizeCollectionIds of the products
_dataAccess.MapProductSizes(products);
// Mass update and insert missing parent records to the database
_dataAccess.UpdateParents(products);
// Get ids of parent products that were newly inserted
_dataAccess.PopulateParentProductByParentSku(products);
// Insert children into database
_dataAccess.InsertProducts(products);
// Insert the UPCs into the database
_dataAccess.InsertUPCs(products);
// Get Product Ids of newly inserted records
_dataAccess.PopulateProductIds(products);
// Get just the parent products to insert the brands
List<ParentProduct> parents = (from prod in products
select prod.ParentProduct).Distinct().ToList();
// Insert ParentProductBrand record
_dataAccess.InsertParentProductBrands(parents);
// Insert the custom attribute records
_dataAccess.InsertProductCustomAttributes(products);
transactionScope.Complete();
}
我的意图是,如果在事务范围内调用的方法中的任何地方发生错误,则事务将回滚,但经过一些测试,似乎情况并非如此,我的数据最终还是半成品。有什么我想念的吗?我是否必须在自己的 TransactionScope 中将数据访问调用包装在方法本身中才能使其正常工作?