Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support:
Entity Framework by default wraps Insert, Update or Delete operation in a transaction, whenever you execute SaveChanges(). EF starts a new transaction for each operation and completes the transaction when the operation finishes. When you execute another such operation, a new transaction is started.
EF 6 has introduced database.BeginTransaction and Database.UseTransaction to provide more control over transactions. Now, you can execute multiple operations in a single transaction as shown below:
using (System.Data.Entity.DbContextTransaction dbTran = context.Database.BeginTransaction( ))
{
try
{
Student std1 = new Student() { StudentName = "newstudent" };
context.Students.Add(std1);
context.Database.ExecuteSqlCommand(
@"UPDATE Student SET StudentName = 'Edited Student Name'" +
" WHERE StudentID =1"
);
context.Students.Remove(std1); //saves all above operations within one transaction
context.SaveChanges(); //commit transaction
dbTran.Commit();
}
catch (Exception ex)
{
//Rollback transaction if exception occurs
dbTran.Rollback();
} }
database.UseTransaction allows the DbContext to use a transaction which was started outside of the Entity Framework.
Download DB First sample project for Transactions demo.
Entity Framework 6.0 Tutorials(6):Transaction support的更多相关文章
- Entity Framework 6.0 Tutorials(1):Introduction
以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...
- Entity Framework 6.0 Tutorials(4):Database Command Logging
Database Command Logging: In this section, you will learn how to log commands & queries sent to ...
- Entity Framework 6.0 Tutorials(11):Download Sample Project
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...
- Entity Framework 6.0 Tutorials(10):Index Attribute
Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...
- Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping
Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...
- Entity Framework 6.0 Tutorials(3):Code-based Configuration
Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...
- Entity Framework 6.0 Tutorials(2):Async query and Save
Async query and Save: You can take advantage of asynchronous execution of .Net 4.5 with Entity Frame ...
- Entity Framework 6.0 Tutorials(8):Custom Code-First Conventions
Custom Code-First Conventions: Code-First has a set of default behaviors for the models that are ref ...
- Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange
DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...
随机推荐
- 1.使用Fiddler进行接口测试
1.Fiddler既可以用来抓包数据,亦可以进行接口测试.(可参考我的另一篇博客:https://www.cnblogs.com/android-it/p/9523548.html 进行接口的编写) ...
- substr的学习(PHP学习)
substr的用法: 首先看PHP手册 ,手册上是这样说的 string substr ( string $string , int $start [, int $length ] ) 执行subst ...
- python urllib和urllib3包
urllib.request urllib当中使用最多的模块,涉及请求,响应,浏览器模拟,代理,cookie等功能. 1. 快速请求 urlopen返回对象提供一些基本方法: read 返回文本数据 ...
- Linux: How to delete a disk or LUN reference from /dev
In AIX, there is rmdev command to remove a disk/LUN from /dev directory i.e to make the disk/LUN una ...
- selenium定位方式源码的存放位置
find_element方法源码存在位置 by定位方法
- 【转】学习使用Jmeter做压力测试(二)--压力测试的实施
JMeter测试步骤: 1.建立测试计划 2.添加线程组 3.添加HTTP请求 4.增加监听器 5.执行测试计划 6.根据JMeter提供的报告分析结果 一.目标 测试访问目标服务器网站首页的每秒查询 ...
- 2012_p2 寻宝 (treasure.cpp/c/pas)
2012_p2 寻宝 (treasure.cpp/c/pas) 时间限制: 1 Sec 内存限制: 128 MB提交: 23 解决: 9[提交][状态][讨论版][命题人:外部导入] 题目描述 2 ...
- vs2017发布web项目
1.webform项目发布 1.右键选择项目. 2.选择自定义. 3.“配置文件名称”,发布后生成在项目中记录此次发布选择的配置信息文件名,下次发布默认为此次选择的配置信息,可以删除,随便填一个. 4 ...
- 密码生成工具Cupp
Cupp可根据已知信息生成相应的字典,用来爆破很有帮助 首先先安装一下cupp 命令:apt-get install cupp 参数说明: -v查看cupp版本号 -h 查看参数列表 -l 从gith ...
- CAD库中统计PBN运行航路条数和总距离
select 'PBN运行航路' 类型, fb.b 总条数, fa.a 总距离 from ( select sum(s) a from ...