码迷,mamicode.com
首页 > 其他好文 > 详细

EF 多库事务

时间:2020-03-06 17:33:59      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:ons   lse   false   class   数据   complete   更改   回滚   har   

using (TransactionScope scope = new TransactionScope())
{
    //Do something with context1
    //Do something with context2

    //Save Changes but don‘t discard yet
    context1.SaveChanges(false);

    //Save Changes but don‘t discard yet
    context2.SaveChanges(false);

    //if we get here things are looking good.
    scope.Complete();
    context1.AcceptAllChanges();
    context2.AcceptAllChanges();

}

 我们用SaveChanges(false)先将必要的数据库操作命令发送给数据库,这是注意context1与context2并没有真正发生改变,如果事务终止,自动回滚,两者的更改都没有真正提交到数据库,所以是可以成功回滚的。

EF 多库事务

标签:ons   lse   false   class   数据   complete   更改   回滚   har   

原文地址:https://www.cnblogs.com/netcs/p/12427398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!