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 ...
随机推荐
- BZOJ4560 [JLoi2016]字符串覆盖
题意 字符串A有N个子串B1,B2,-,Bn.如果将这n个子串分别放在恰好一个它在A中出现的位置上(子串之间可以重叠) 这样A中的若干字符就被这N个子串覆盖了.问A中能被覆盖字符个数的最小值和最大值. ...
- UCloud 云服务器硬盘扩容后 如何挂载到本机
UCloud 云服务器硬盘扩容后如何挂载到本机 UCloud 提供的云服务器会根据不同的系统初始化不同空间大小的硬盘资源,此资源默认为 系统盘. 针对 Linux 系统默认初始化 20G 的空间,一般 ...
- JAVA中常见异常类
1. java.lang.nullpointerexception 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对 ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- fragment在水平/垂直时的应用
直接看代码 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedIns ...
- VS2010环境下MFC使用DataGrid绑定数据源
如果MFC的软件中 使用DataGrid控件后,在别的电脑上不能运行行,需要拷贝一个 MSDATGRD.ocx 和msstdfmt.dll 文件在软件的目录中,并写一个批处理文件 reg.dat 文 ...
- ZPAY个人收款助手使用说明
ZPAY个人收款助手使用说明 功能特点: ZPAY个人收款助手可实现收款成功后发送通知到服务器,网页可从服务器获取到付款状态从而完成操作. 可支持微信,支付宝的个人收款需求,无需支付宝微信认证,无需上 ...
- (转)oracle嵌套表示例
本文转载自:http://www.cnblogs.com/gisdream/archive/2012/04/13/2445291.html ----嵌套表:就是把一个表中的字段定义为一个表,这个字段表 ...
- 【转】 Pro Android学习笔记(九五):AsyncTask(4):执行情况
目录(?)[-] 两个AsyncTask对象的运行情况 多次执行的异常 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/ ...
- java代码水仙花
总结:分离出百位,十位,各位,我总是模模糊糊的,总是分不清取膜与除号的作用区别: “%”的意思是“取膜”,表示取得的是余数 “/”的意思是除,得到的是除数. package com.a; //求水仙花 ...