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文件,编译后 ...
随机推荐
- Chrome disable cache & clear memory cache
Chrome disable cache & clear memory cache disable cache
- [Java] Thread的start()和run()函数区别
1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码: 通过调用Thread类的start()方法来启动一个线程,这时此线程是处于就绪状 ...
- 在使用ADOQuery删除数据时的处理 [问题点数:100分,结帖人isdxsc]
在使用ADOQuery删除数据时的,希望在他的事件BeforeDelete进行一些判断,符合要求的进行删除,不符合要求的终止这个删除行为,请问应该用什么语句呢?还有个比较奇怪的现象也一起请教:DBGr ...
- BZOJ3653谈笑风生——可持久化线段树+dfs序
题目描述 设T 为一棵有根树,我们做如下的定义: ? 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道 高明到哪里去了”. ? 设a 和 b 为 T 中的两个不同节点.如果 a ...
- webapi 405 method not allowed
问题的原因:创建webapi controller时,习惯创建了mvc的controller,而非api controller.导致引用包有问题. 这两天搞webapi开发的时候,遇见了405错误. ...
- 【 Gym - 101124E 】Dance Party (数学)
BUPT2017 wintertraining(15) #4G Gym - 101124 E.Dance Party 题意 有c种颜色,每个颜色最多分配给两个人,有M个男士,F个女士,求至少一对男士同 ...
- 洛谷P1516 青蛙的约会(扩展欧几里德)
洛谷题目传送门 很容易想到,如果他们相遇,他们初始的位置坐标之差\(x-y\)和跳的距离\((n-m)t\)(设\(t\)为跳的次数)之差应该是模纬线长\(l\)同余的,即\((n-m)t\equiv ...
- AI将带我们走向何方?
AI即人工智能,对科幻着迷的博主对此认知颇深,打算从科幻电影入手,先讲下未来的AI将给人类带来哪些变化,哪些思考. 从最初的<星际航行>中的各种星球.地形等的介绍,到各个鉴于的探索,以及其 ...
- 06 Zabbix分布式监控和主被动模式
06 Zabbix分布式监控和主被动模式 zabbix proxy设置 使用zabbix代理的好处 监控拥有不可靠的远程区域 当监控项目数以万计的时候使用代理分担zabbix-proxy压力 简化分布 ...
- LoadRunner回放乱码
乱码的原因和服务器字符集编码相关,默认LR是gbk解码,如果服务器是utf8,那么需要改为utf8解码,这样中文就显示正常了. 通过查看源码,可以知道服务器的编码方式:<meta charset ...