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

mac2022-06-30  23

使用 前文中描述的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(); }

 

 

转载于:https://www.cnblogs.com/zoro-zero/p/4688965.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)