EntityFramework用法探索(八)事务处理

简介:

使用前文中描述的Retail示例,在Customer对象的Mapping中设置Name属性:

1       this.Property(t => t.Name)
2           .IsRequired()
3           .HasMaxLength(256);

要求该属性不能为空,并且长度不超过256。

我们构造一个有效的Customer对象,再构造一个无效的Name属性为空的对象。

复制代码
 1       DomainModels.Customer customer1 = new DomainModels.Customer()
 2       {
 3         Name = "Dennis Gao",
 4         Address = "Beijing",
 5         Phone = "18888888888",
 6       };
 7       DomainModels.Customer customer2 = new DomainModels.Customer()
 8       {
 9         //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空
10         Address = "Beijing",
11         Phone = "16666666666",
12       };
复制代码

我们使用如下代码添加Customer对象数据到数据库中,

复制代码
 1       Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1);
 2       Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2);
 3 
 4       using (RetailEntities context = new RetailEntities())
 5       {
 6         context.Customers.Add(entity1);
 7         context.Customers.Add(entity2);
 8         context.SaveChanges(); // 提交时将抛出异常
 9 
10         customer1.Id = entity1.Id;
11         customer2.Id = entity2.Id;
12       }
13 
14       Console.WriteLine(customer1);
15       Console.WriteLine(customer2);
复制代码

得到结果:

EntityFramework已经明确的告诉我们某Entity验证失败。此时查询数据库,两条记录均不存在。EntityFramework自带的事务已经帮助回滚了操作。

现在我们修改下程序,改为提交两次:

复制代码
 1       try
 2       {
 3         using (RetailEntities context = new RetailEntities())
 4         {
 5           context.Customers.Add(entity1);
 6           context.SaveChanges(); // 顺利执行
 7           context.Customers.Add(entity2);
 8           context.SaveChanges(); // 提交时将抛出异常
 9 
10           customer1.Id = entity1.Id;
11           customer2.Id = entity2.Id;
12         }
13       }
14       catch (Exception ex)
15       {
16         Console.WriteLine(FlattenException(ex));
17       }
复制代码

此时得到结果:

通过查询,可以确定,第一条数据已经被成功保存。

然后我们修改代码,增加TransactionScope,

复制代码
 1         using (var transactionScope = new TransactionScope(
 2           TransactionScopeOption.RequiresNew))
 3         {
 4           Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1);
 5           Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2);
 6 
 7           using (RetailEntities context = new RetailEntities())
 8           {
 9             context.Customers.Add(entity1);
10             context.SaveChanges(); // 顺利提交
11             context.Customers.Add(entity2);
12             context.SaveChanges(); // 提交时将抛出异常
13 
14             customer1.Id = entity1.Id;
15             customer2.Id = entity2.Id;
16           }
17 
18           transactionScope.Complete();
19         }
20       }
21       catch (Exception ex)
22       {
23         Console.WriteLine(FlattenException(ex));
24       }
复制代码

此时,仍然在第二个SaveChanges()处抛出异常,但第一个SaveChanges()的提交也被回滚了。

证明事务Scope起到了作用。

完整代码和索引

EntityFramework用法探索系列

完整代码下载





本文转自匠心十年博客园博客,原文链接:http://www.cnblogs.com/gaochundong/archive/2013/06/09/entityframework_usage_transaction_scope.html,如需转载请自行联系原作者


目录
相关文章
|
4月前
|
SQL 存储 开发框架
EntityFramework数据持久化复习资料6、EntityFramework引入
EntityFramework数据持久化复习资料6、EntityFramework引入
41 0
|
4月前
|
SQL 存储 开发框架
EntityFramework数据持久化复习资料1、委托
EntityFramework数据持久化复习资料1、委托
53 0
|
关系型数据库 MySQL PHP
MySQL事务的四种隔离类型以及PHP框架Yii2中的源码解读和实际应用
MySQL事务的四种隔离类型以及PHP框架Yii2中的源码解读和实际应用
336 0
|
SQL 关系型数据库 PHP
thinkphp数据库事务例子
thinkphp数据库事务例子
114 0
|
SQL 数据库
EFCore-3
建模数据库,目前EFCore只支持Code First方法。
129 0
|
SQL 缓存 关系型数据库
深入理解Yii2 数据库事务
  事务(Transaction)   在Yii中,使用 yii\db\Transaction 来表示数据库事务。   一般情况下,我们从数据库连接启用事务,通常采用如下的形式:   $transaction=$connection->beginTransaction();   try {   $connection->createCommand($sql1)->execute();
419 0
|
SQL
数据访问函数库的使用方法(二)—— 获取记录集和使用事务的方法
使用SQL语句来获取记录集的方法 string sql = "select col1,col2,col3  from TableName where ";            //获取DataTable            DataTable dt = dal.
887 0
|
数据库 C# 数据库连接
TransactionScope事务处理方法介绍及.NET Core中的注意事项
作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/10170712.html 今天在写CzarCms的UnitOfWork的使用使用到了这个TransactionScope事务,因此对它进行了相关资料的查阅并记录如下,希望对大伙在.NET Core中使用有所帮助。
4471 0