EntityFramework用法探索(八)交易處理

來源:互聯網
上載者:User

使用前文中描述的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用法探索系列

  • (一)DatabaseFirst
  • (二)CodeFirst
  • (三)CodeFirst流暢API
  • (四)Repository和UnitOfWork
  • (五)引入Unity
  • (六)靜態Repository
  • (七)安全執行緒實踐
  • (八)交易處理

完整代碼下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.