Entity Framework中的批量提交与事务处理
来源:互联网 发布:淘宝小号怎么养2017 编辑:程序博客网 时间:2024/06/07 08:34
在Entity Framework 中使用SaveChanges()是很频繁的,单次修改或删除数据后调用SaveChanges()返回影响记录数。
要使用批量修改或者批量删除数据,就需要SaveChanges(false)+AcceptAllChanges()方法了。
SaveChanges(false) 只是通知EF需要对数据库执行的操作,在内存中是属于挂起状态,在必要的时候是可以撤销的,比如AcceptAllChange()提交为真正成功,EF将撤销SaveChanges(false)的操作。
而在处理分布式事务操作的时候,就有必要使用TransactionScope 来处理了,很多时候我们会这样写:
using (TransactionScope scope = new TransactionScope()){ //Do something with context1 //Do something with context2 //Save and discard changes context1.SaveChanges(); //Save and discard changes context2.SaveChanges(); //if we get here things are looking good. scope.Complete();}
如context1.SaveChanges()成功了,context2.SaveChanges()却是有问题的,我们在scope.Complete()提交事务的时候就会终止,而Context1已经成功执行了
这可能不一定符合我们的需要。如果我们需要 context1、context2要不同时执行成功,要不都不成功,我们需要对代码作小小的调整,如用下面的代码:
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();}
在Entity Framework 中使用SaveChanges()是很频繁的,单次修改或删除数据后调用SaveChanges()返回影响记录数。
要使用批量修改或者批量删除数据,就需要SaveChanges(false)+AcceptAllChanges()方法了。
SaveChanges(false) 只是通知EF需要对数据库执行的操作,在内存中是属于挂起状态,在必要的时候是可以撤销的,比如AcceptAllChange()提交为真正成功,EF将撤销SaveChanges(false)的操作。
而在处理分布式事务操作的时候,就有必要使用TransactionScope 来处理了,很多时候我们会这样写:
using (TransactionScope scope = new TransactionScope()){ //Do something with context1 //Do something with context2 //Save and discard changes context1.SaveChanges(); //Save and discard changes context2.SaveChanges(); //if we get here things are looking good. scope.Complete();}
如context1.SaveChanges()成功了,context2.SaveChanges()却是有问题的,我们在scope.Complete()提交事务的时候就会终止,而Context1已经成功执行了
这可能不一定符合我们的需要。如果我们需要 context1、context2要不同时执行成功,要不都不成功,我们需要对代码作小小的调整,如用下面的代码:
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();}
To use TransactionScope class you need to keep two things in mind
- You need to add System.Transactions reference to your project
- Make sure the Windows service “Distributed Transaction Coordinator” is up and running.
- Entity Framework中的批量提交与事务处理
- Entity Framework中的批量提交与事务处理
- Entity Framework4.5及之前的批量提交与事务处理
- Entity Framework 批量删除
- entity framework 批量删除
- entity framework 批量操作
- Entity Framework中的事务
- ASP.NET 4.0 与 Entity Framework 4-第四篇-Entity Framework在三层架构中的使用
- MYSQL与Entity Framework
- .Net Framework 2.0 中的事务处理
- 关于Entity Framework中的CreatedTime
- Entity Framework 中的Code First
- Linq与EF(Entity Framework)
- Entity Framework安装与使用
- BingMap与Entity Framework,保存与读取
- 关于Entity Framework 学习中的POCO
- Entity Framework在三层架构中的使用
- 浅谈Entity Framework中的数据加载方式
- HDU 1789(贪心题)
- OpenGL程序中与glew相关的未处理异常的解决方案
- C语言指针开辟内存释放内存
- 数据结构之单链表实现栈(C++)
- 关于QT的一些资料分享
- Entity Framework中的批量提交与事务处理
- 第一章_概述
- 历数那些失败的项目(4)---CMS网站
- 【转载】也有很多的例子飞鸽传书
- 2013.05.07总结与计划
- rc.d相关
- Asp.net使用ajax无刷新上传文件(附源码)
- 【转载】送到榨油厂的飞鸽传书
- 利用 Qt 4.1.0 读取注册表获得串口信息