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. DbEntry 简单实现

    在着手编码之前首先安装DbEntry DbEntry.Net.4.1.Setup.zip 在建立类库时选择 DbEntryClassLibrary 如图 DbEntryClassLibrary1 中建 ...

  2. 关于quartus工程添加文件的说明

    quartus工程中要添加bsf文件的话需要将源文件也一同添加进来,添加ip核需要添加qip文件,时序约束文件只有添加到工程中才有效果,而timequest分析时需要制定约束文件.

  3. JAVA单例模式:懒汉式,饿汉式

    今天复习了一下java的单例模式,写了懒汉式和饿汉式的实现例子.代码如下: 1.懒汉式单例 package com.lf.shejimoshi; /** * @classDesc: 类描述:(懒汉式单 ...

  4. ③SpringBoot中Redis的使用

    本文基于前面的springBoot系列文章进行学习,主要介绍redis的使用. SpringBoot对常用的数据库支持外,对NoSQL 数据库也进行了封装自动化. redis介绍 Redis是目前业界 ...

  5. (转)RadioButton左侧显示文字,右侧显示按钮时文字不靠边的问题解决

    作者:  发布日期:2014-02-13 21:00:45 我来说两句(0) 0 Tag标签:RadioButton  左侧  显示   项目中有一个这样的需求: 下面三行点击某行即选中,颜色变深.自 ...

  6. LaunchImage添加以及设置无效处理

    1.添加LaunchImage 2.添加所需要图片即可,出现un..可以删除,警告也随之而去,并删除LauchImage Assets之后重新添加 3.确定设置是否一样 4.发现启动后加载不了启动图, ...

  7. OPCDAAuto.dll的C#使用方法浅析

    上次研究了.Net版本的OPC API dll,这次我采用OPCDAAuto.dll来介绍使用方法.以下为我的源代码,有详细的注释无需我多言.编译平台:VS2008SP1.WINXP.KEPServe ...

  8. Erlang基础 -- 介绍 -- Erlang特点

    前言 Erlang是具有多重范型的编程语言,具有很多特点,主要的特点有以下几个: 函数式 并发性 分布式 健壮性 软实时 热更新 递增式代码加载 动态类型 解释型 函数式 Erlang是函数式编程语言 ...

  9. 3 SpringBoot与微服务

    SpringBoot的使用? 化繁为简: Spring MVC 需要定义各种配置,配置文件多. SpringBoot的核心功能? 独立运行: java -jar XXX.jar (以前启动SPring ...

  10. 代码说明call和apply方法的区别 (咱们这方面讲解的少,这样的题有变式,需要举例讲解一下)

    这两个都是函数的方法,可以改变this的指向,fn.call(obj,param1,param2,…) call传入单个参数 fn.apply(obj,[param1,param2,...]) app ...