Background:
I have an object that has a number of properties that are other objects. On Save of the main object, I insert a record into the database and then call the Save method of the on the sub objects. The current risk in this model is that if the main object is saved to the database, but one of the sub-objects are not, then the data will not reflect an accurate state.
我有一个对象有许多属性是其他对象。在保存主对象时,我将一条记录插入数据库,然后调用子对象上的Save方法。此模型中的当前风险是,如果主对象保存到数据库,但其中一个子对象没有,那么数据将不会反映准确的状态。
My .NET 4.0 application uses Enterprise Library 5.0 and the data is stored in one SQL Server 2008 database. I would like to implement transaction support so that either all the objects are saved when all saves are successful, or rolled back on failure.
我的.NET 4.0应用程序使用Enterprise Library 5.0,数据存储在一个SQL Server 2008数据库中。我想实现事务支持,以便在所有保存成功时保存所有对象,或者在失败时回滚。
Question:
I found a good but dated article about Transaction support in .NET 2.0 vs .NET 1.0/1.1 but wanted to know what the current best practice is to implement transaction support.
我发现了一篇关于.NET 2.0与.NET 1.0 / 1.1中的事务支持的好文章,但想知道当前最佳实践是实现事务支持的。
1 个解决方案
#1
1
I ended up going with System.Transactions.TransactionScope as outlined in the article referenced above. Enterprise Library 3.0 and higher looks for Transaction.Current when connecting to the database. You don't have to explicitly pass transaction information.
我最后使用上面引用的文章中概述的System.Transactions.TransactionScope。 Enterprise Library 3.0及更高版本在连接到数据库时查找Transaction.Current。您不必显式传递交易信息。
using(TransactionScope scope = new TransactionScope())
{
/* Perform transactional work here */
//No errors-commit transaction
scope.Complete();
}
#1
1
I ended up going with System.Transactions.TransactionScope as outlined in the article referenced above. Enterprise Library 3.0 and higher looks for Transaction.Current when connecting to the database. You don't have to explicitly pass transaction information.
我最后使用上面引用的文章中概述的System.Transactions.TransactionScope。 Enterprise Library 3.0及更高版本在连接到数据库时查找Transaction.Current。您不必显式传递交易信息。
using(TransactionScope scope = new TransactionScope())
{
/* Perform transactional work here */
//No errors-commit transaction
scope.Complete();
}