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的更多相关文章

  1. Entity Framework 6.0 Tutorials(1):Introduction

    以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...

  2. 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 ...

  3. Entity Framework 6.0 Tutorials(11):Download Sample Project

    Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...

  4. Entity Framework 6.0 Tutorials(10):Index Attribute

    Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...

  5. Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping

    Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...

  6. Entity Framework 6.0 Tutorials(3):Code-based Configuration

    Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...

  7. 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 ...

  8. 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 ...

  9. 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. SqlServer 数据库读写分离【转】

    1. 实现原理:读写分离简单的说是把对数据库读和写的操作分开对应不同的数据库服务器,这样能有效地减轻数据库压力,也能减轻io压力.主数据库提供写操作,从数据库提供读操作,其实在很多系统中,主要是读的操 ...

  2. 关于fpga的m9k的部分理解

    1.控制信号包括时钟使能,读写使能,字节使能,地址使能,异步清零等 2.可配置为单端口,简单双端口,真双端口,fifo,rom,移位寄存器. 3.关于移位寄存器模式的介绍如下: 一个 ( w × m ...

  3. keycloak && docker安装 &&spring boot 集成使用

    1. 基础依赖 a. docker mysql b. dokcer keycloak-mysql   2. 安装     mysql (注意实际使用最好使用本地数据卷) docker run --na ...

  4. 5 数组 Swift/Object-C ——《Swift3.0从入门到出家》

    Swift中数组是一种数据结构,用来存放多个形同类型的数据结构,数据在数组内的存放是有序的,存进来的数据个读出来的顺序相同 Object-C 中数组能够存放任意类型的数据类型为[AnyObject] ...

  5. Aes加密算法加密模式介绍

    本文转自:https://www.jianshu.com/p/582d3a47729a AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中 ...

  6. 连续创建多个Oracle触发器失败,单个创建才成功的解决方法

    当用到自增序列,需要用到触发器的时候,当我连续执行创建多个触发器时,总是报编译通过,但存在警告或错误. ---.应用信息 drop table app_info cascade constraints ...

  7. python基础篇之进阶

    python基础篇之进阶 参考博客:http://www.cnblogs.com/wupeiqi/articles/5115190.html python种类 1. cpython  使用c解释器生产 ...

  8. CAN总线扩展数据帧介绍

    在扩展CAN 数据帧中,紧随SOF 位的是32 位的仲裁字段.仲裁字段的前11 位为29 位标识符的最高有效位(Most Significant bit,MSb)(基本lD) .紧随这11 位的是替代 ...

  9. 低成本FPGA中实现动态相位调整

    在FPGA中,动态相位调整(DPA)主要是实现LVDS接口接收时对时钟和数据通道的相位补偿,以达到正确接收的目的.ALTERA的高端FPGA,如STRATIX(r) 系列中自带有DPA电路,但低端的F ...

  10. kali 软件源 包含virtualbox所需头文件

    # deb cdrom:[Debian GNU/Linux 7.0 _Kali_ - Official Snapshot i386 LIVE/INSTALL Binary 20130905-08:50 ...