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. ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...

  2. 封装Socket.BeginReceive/EndReceive以支持Timeout

    Socket .NET中的Socket类提供了网络通信常用的方法,分别提供了同步和异步两个版本,其中异步的实现是基于APM异步模式实现,即BeginXXX/EndXXX的方式.异步方法由于其非阻塞的特 ...

  3. Win7旗舰版一直显示检查更新的问题

    最近部门机器从新安装win7 64位 旗舰版之后,每次检查更新,都会一直卡在检查更新的界面过不去,上网搜了一下,看到了网友提供的解决办法, 测试了一下,果然可以.记录下来,以备后用: 到微软官网去下载 ...

  4. npm run dev报错,events.js:160 throw er; // Unhandled 'error' event

    错误代码如下: vue-project@1.0.0 dev E:MySoftwaretestGitwebpackvue-projectnode build/dev-server.js "80 ...

  5. 【转】Jmeter压力测试模拟并发

    jmeter下载地址:http://jmeter.apache.org/download_jmeter.cgi JMeterPlugins(jmeter插件):http://jmeter-plugin ...

  6. pytest命令行选项

    -m 标记 代码加一个装饰器:@pytest.mark.run_bbc_test,命令行添加 -m run_bbc_test,执行带@pytest.mark.run_bbc_test的测试用例: -k ...

  7. 银行排队问题之单队列多窗口加VIP服务(30 分)

    银行排队问题之单队列多窗口加VIP服务(30 分) 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选 ...

  8. 1048 Find Coins

    题意:略 思路:two pointers思想,简单 先对数字序列进行排序,然后定义两个指针left和right,初始状态low=0,high=n-1.当a[low]+a[high] > M时,h ...

  9. 前端自动化工具 -- gulp https://angularjs.org/

    gulp是基于流的前端自动化构建工具. gulp是基于stream流的形式,也就是前一个函数(工厂)制造出结果,提供后者使用. 同样的,也是包括基本用法和各插件的使用. 二.基本用法--插件使用 gu ...

  10. String to Integer (atoi) ???

    #define INT_MAX 2147483647 #define INT_MIN -2147483648 class Solution { public: int atoi(const char ...