zoukankan      html  css  js  c++  java
  • 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用法探索系列

    完整代码下载

  • 相关阅读:
    定位CPU占用高的线程并打印其堆栈信息
    单生产者和单消费者共同操作同一个消息队列需要加锁吗
    cmake多目录,生成so的模板
    【秒懂音视频开发】25_H.264解码实战
    【秒懂音视频开发】24_H.264编码实战
    【秒懂音视频开发】23_H.264编码
    【秒懂音视频开发】22_显示YUV图片
    【秒懂音视频开发】21_显示BMP图片
    【秒懂音视频开发】20_视频录制02_编程
    【秒懂音视频开发】19_视频录制01_命令行
  • 原文地址:https://www.cnblogs.com/gaochundong/p/entityframework_usage_transaction_scope.html
Copyright © 2011-2022 走看看