使用前文中描述的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
- (七)安全執行緒實踐
- (八)交易處理
完整代碼下載