我有这样的代码:
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 中才能使其正常工作?
最佳答案
看起来在您的数据访问层中创建了多个数据库连接实例。尝试在 DataAccess 类构造函数中实例化您的数据库连接,并在您的 DataAccess 方法中使用它。您可能想阅读 this blog post
关于c# - 跨多个方法的 TransactionScope 不是 ACID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11130155/