如何使用实体框架的交易? [英] How to use transactions with the Entity Framework?
本文介绍了如何使用实体框架的交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当你有这样的代码:
Something something = new Something();
BlahEntities b = new BlahEntities()
b.AddToSomethingSet(something);
b.SaveChanges();
如何在交易中运行该添加?
how do run that addition inside a transaction?
推荐答案
您可以将代码放在事务范围内。
You can place your code within a Transaction scope
using(TransactionScope scope = new TransactionScope())
{
// Your code
scope.Complete(); // To commit.
}
TransactionScope位于System.Transactions命名空间中,该空间位于相同的名称(您可能需要手动添加到项目中)。
TransactionScope is in the System.Transactions namespace which is located in the assembly of the same name (which you may need to add manually to your project).
这篇关于如何使用实体框架的交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文