EntityFramework用法探索(八)事务处理
使用 前文中描述的Retail示例 ,在Customer对象的Mapping中设置Name属性:
我们构造一个有效的Customer对象,再构造一个无效的Name属性为空的对象。
DomainModels.Customer customer1 = new DomainModels.Customer()
{
Name = "Dennis Gao",
Address = "Beijing",
Phone = "",
};
DomainModels.Customer customer2 = new DomainModels.Customer()
{
//Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空
Address = "Beijing",
Phone = "",
};
我们使用如下代码添加Customer对象数据到数据库中,
Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1);
Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2); using (RetailEntities context = new RetailEntities())
{
context.Customers.Add(entity1);
context.Customers.Add(entity2);
context.SaveChanges(); // 提交时将抛出异常 customer1.Id = entity1.Id;
customer2.Id = entity2.Id;
} Console.WriteLine(customer1);
Console.WriteLine(customer2);
EntityFramework已经明确的告诉我们某Entity验证失败。此时查询数据库,两条记录均不存在。EntityFramework自带的事务已经帮助回滚了操作。
现在我们修改下程序,改为提交两次:
try
{
using (RetailEntities context = new RetailEntities())
{
context.Customers.Add(entity1);
context.SaveChanges(); // 顺利执行
context.Customers.Add(entity2);
context.SaveChanges(); // 提交时将抛出异常 customer1.Id = entity1.Id;
customer2.Id = entity2.Id;
}
}
catch (Exception ex)
{
Console.WriteLine(FlattenException(ex));
}
然后我们修改代码,增加TransactionScope,
实例1
using (var transactionScope = new TransactionScope(
TransactionScopeOption.RequiresNew))
{
Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1);
Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2); using (RetailEntities context = new RetailEntities())
{
context.Customers.Add(entity1);
context.SaveChanges(); // 顺利提交
context.Customers.Add(entity2);
context.SaveChanges(); // 提交时将抛出异常 customer1.Id = entity1.Id;
customer2.Id = entity2.Id;
} transactionScope.Complete();
}
}
catch (Exception ex)
{
Console.WriteLine(FlattenException(ex));
}
实例2
using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew))
{ //收藏
string collSql = @"delete from CollectDiscoverInfo where DiscoverID='{0}'"; collSql = string.Format(collSql, id);
db.Database.ExecuteSqlCommand(collSql); //评论
string commentSql = "delete from CommentDiscoverInfo where DiscoverID='{0}'";
commentSql = string.Format(commentSql, id);
db.Database.ExecuteSqlCommand(commentSql); //赞
string praiseSql = "delete from PraiseDiscover where DiscoverID='{0}'";
praiseSql = string.Format(praiseSql, id);
db.Database.ExecuteSqlCommand(praiseSql); //图片
string photoSql = "delete from DiscoverPhotoInfo where DiscoverID='{0}'"; photoSql = string.Format(photoSql, id);
db.Database.ExecuteSqlCommand(photoSql); //话题
db.DiscoverInfo.Remove(entity);
db.SaveChanges(); //提交事务
transactionScope.Complete();
}
EntityFramework用法探索(八)事务处理的更多相关文章
- .net EntityFramework用法探索系列 1
EntityFramework用法探索系列 (一)DatabaseFirst (二)CodeFirst (三)CodeFirst流畅API (四)Repository和UnitOfWork (五)引入 ...
- CoreCLR源码探索(八) JIT的工作原理(详解篇)
在上一篇我们对CoreCLR中的JIT有了一个基础的了解, 这一篇我们将更详细分析JIT的实现. JIT的实现代码主要在https://github.com/dotnet/coreclr/tree/m ...
- MongoDB 监控 --- MongoDB基础用法(八)
MongoDB 监控 在你已经安装部署并允许MongoDB服务后,你必须要了解MongoDB的运行情况,并查看MongoDB的性能.这样在大流量得情况下可以很好的应对并保证MongoDB正常运作. M ...
- EntityFramework DbContext 线程安全
先看这一段异常信息: A second operation started on this context before a previous asynchronous operation compl ...
- EntityFramework 中支持 BulkInsert 扩展
本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 前言 很显然,你应该不至于使用 EntityFramework 直接插入 10W 数据到数据库中,那 ...
- EntityFramework中支持BulkInsert扩展(转载)
前言 很显然,你应该不至于使用 EntityFramework 直接插入 10W 数据到数据库中,那可能得用上个几分钟.EntityFramework 最被人诟病的地方就是它的性能,处理大量数据时的效 ...
- EntityFramework中支持BulkInsert扩展
EntityFramework中支持BulkInsert扩展 本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 前言 很显然,你应该不至于使用 Ent ...
- Manager(管理器)
Manager(管理器) 索引 意图 结构 参与者 适用性 效果 实现 实现方式(一):Manager 模式的示例实现. 意图 将对一个类的所有对象的管理封装到一个单独的管理器类中. 这使得管理职责的 ...
- Dapper ORM 用法—Net下无敌的ORM(转)
假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载Dapper的优势:1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编译后 ...
随机推荐
- angular4 组件通讯、生命周期
主要通讯形式 父组件通过属性绑定到子组件,子组件通过事件传递参数到父组件 父组件通过局部变量获取子组件的引用 父组件使用@ViewChild获取子组件的引用 两个不相关联的组件使用中间人模式交互 终极 ...
- python学习笔记三——控制语句
2.5 运算符与表达式 2.5.1 算术运算符和算术表达式 算术运算符包括四则运算符.求模运算符和求幂运算符. 算术运算符 加减乘除:+ - * / 表达式:x+y x-y x*y x/y ...
- YII2十三大特性2
第十三 场景(scenario)的使用 例如:有三个场景,分别为创建,更新,确认回款 首先,定义所有的场景,及规则,如下所示: <?php namespace core\models; use ...
- Java常用工具方法
以GET请求形式获取文本文件内容 /** * 以GET请求形式获取文本文件内容 * @param url http下载地址,比如http://www.abc.com/123.css * @return ...
- Popular Cows POJ - 2186(强连通分量)
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...
- Trailing Zeroes (III) LightOJ - 1138(二分)
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...
- 自定义git忽略规则
输入命令 >a.o git status 此时我想让git忽略这个文件的更新 输入命令 > .gitignore echo "a.o" >> .gitign ...
- 【转】IAR for STM8介绍、下载、安装与注册
Ⅰ.写在前面 本文讲述的内容是IAR for STM8(EWSTM8)的介绍.下载.安装与注册,其安装.注册过程和IAR for ARM类似,如果需要了解IAR for ARM相关的文章,可以到我博客 ...
- Java中线程池的实现原理-求职必备
jdk1.5引入Executor线程池框架,通过它把任务的提交和执行进行解耦,只需要定义好任务,然后提交给线程池,而不用关心该任务是如何执行.被哪个线程执行,以及什么时候执行. 初始化线程池(4种) ...
- A1039. Course List for Student
Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists o ...