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

使用 前文中描述的Retail示例 ,在Customer对象的Mapping中设置Name属性:
我们构造一个有效的Customer对象,再构造一个无效的Name属性为空的对象。
复制代码
   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对象数据到数据库中,

复制代码
   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自带的事务已经帮助回滚了操作。

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

复制代码
  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       }
复制代码

 实例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();
                }
复制代码

 

 
posted @   大空白纸  阅读(242)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示