如何在实体框架中使用事务? [英] How to use transactions with the Entity Framework?

查看:17
本文介绍了如何在实体框架中使用事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你有这样的代码时:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆