static void Main(string[] args) { ICustomerRepository customerRepository = new CustomerRepository(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.Red; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; DomainModels.Customer customer2 = new DomainModels.Customer() { //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空 Address = "Beijing", Phone = "16666666666", }; try { Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1); Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2); using (RetailEntities context = new RetailEntities()) { context.Customers.Add(entity1); context.Customers.Add(entity2); using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { context.SaveChanges(); transactionScope.Complete(); } customer1.Id = entity1.Id; customer2.Id = entity2.Id; } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } Console.WriteLine(customer1); Console.WriteLine(customer2); Console.WriteLine("====================================="); // =============== 查询回滚结果 =============== List<DomainModels.Customer> customers = new List<DomainModels.Customer>(); try { using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { using (RetailEntities context = new RetailEntities()) { List<Customer> entities = context.Customers.AsQueryable().ToList(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity); customers.Add(customer); } } transactionScope.Complete(); } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } foreach (var customer in customers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }
static void Main(string[] args) { ICustomerRepository customerRepository = new CustomerRepository(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.Red; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; DomainModels.Customer customer2 = new DomainModels.Customer() { //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空 Address = "Beijing", Phone = "16666666666", }; //Task t = Task.Factory.StartNew(() => //{ // int i = 0; // while (i < 100) // { // List<DomainModels.Customer> customers = new List<DomainModels.Customer>(); // using (var transactionScope = new TransactionScope( // TransactionScopeOption.RequiresNew, // new TransactionOptions() // { // IsolationLevel = IsolationLevel.ReadUncommitted // })) // { // try // { // using (RetailEntities context = new RetailEntities()) // { // List<Customer> entities = context.Customers.AsQueryable().ToList(); // foreach (var entity in entities) // { // DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity); // customers.Add(customer); // } // } // } // catch (Exception ex) // { // Console.WriteLine(FlattenException(ex)); // } // transactionScope.Complete(); // } // Console.WriteLine("-----> " + i + " times"); // foreach (var customer in customers) // { // Console.WriteLine(customer); // } // i++; // Thread.Sleep(1000); // } //}); try { using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew)) { Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1); Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2); using (RetailEntities context = new RetailEntities()) { context.Customers.Add(entity1); context.SaveChanges(); // 顺利提交 context.Customers.Add(entity2); context.SaveChanges(); // 提交时将抛出异常 customer1.Id = entity1.Id; customer2.Id = entity2.Id; } transactionScope.Complete(); } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } Console.WriteLine(customer1); Console.WriteLine(customer2); Console.WriteLine("====================================="); // =============== 查询回滚结果 =============== List<DomainModels.Customer> getCustomers = new List<DomainModels.Customer>(); using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { try { using (RetailEntities context = new RetailEntities()) { List<Customer> entities = context.Customers.AsQueryable().ToList(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity); getCustomers.Add(customer); } } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } transactionScope.Complete(); } foreach (var customer in getCustomers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }
static void Main(string[] args) { ICustomerRepository customerRepository = new CustomerRepository(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.Red; DomainModels.Customer customer1 = new DomainModels.Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; DomainModels.Customer customer2 = new DomainModels.Customer() { //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空 Address = "Beijing", Phone = "16666666666", }; //Task t = Task.Factory.StartNew(() => //{ // int i = 0; // while (i < 100) // { // List<DomainModels.Customer> customers = new List<DomainModels.Customer>(); // using (var transactionScope = new TransactionScope( // TransactionScopeOption.RequiresNew, // new TransactionOptions() // { // IsolationLevel = IsolationLevel.ReadUncommitted // })) // { // try // { // using (RetailEntities context = new RetailEntities()) // { // List<Customer> entities = context.Customers.AsQueryable().ToList(); // foreach (var entity in entities) // { // DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity); // customers.Add(customer); // } // } // } // catch (Exception ex) // { // Console.WriteLine(FlattenException(ex)); // } // transactionScope.Complete(); // } // Console.WriteLine("-----> " + i + " times"); // foreach (var customer in customers) // { // Console.WriteLine(customer); // } // i++; // Thread.Sleep(1000); // } //}); try { using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew)) { Customer entity1 = Mapper.Map <DomainModels.Customer, Customer>(customer1); Customer entity2 = Mapper.Map <DomainModels.Customer, Customer>(customer2); using (RetailEntities context = new RetailEntities()) { context.Customers.Add(entity1); context.SaveChanges(); // 顺利提交 context.Customers.Add(entity2); context.SaveChanges(); // 提交时将抛出异常 customer1.Id = entity1.Id; customer2.Id = entity2.Id; } transactionScope.Complete(); } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } Console.WriteLine(customer1); Console.WriteLine(customer2); Console.WriteLine("====================================="); // =============== 查询回滚结果 =============== List <DomainModels.Customer> getCustomers = new List <DomainModels.Customer>(); using (var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { try { using (RetailEntities context = new RetailEntities()) { List <Customer> entities = context.Customers.AsQueryable().ToList(); foreach (var entity in entities) { DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity); getCustomers.Add(customer); } } } catch (Exception ex) { Console.WriteLine(FlattenException(ex)); } transactionScope.Complete(); } foreach (var customer in getCustomers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }
static void Main(string[] args) { ICustomerRepository customerRepository = new CustomerRepository(); // =============== 增 =============== Console.ForegroundColor = ConsoleColor.DarkRed; Customer customer1 = new Customer() { Name = "Dennis Gao", Address = "Beijing", Phone = "18888888888", }; customerRepository.InsertCustomer(customer1); Console.WriteLine(customer1); Customer customer2 = new Customer() { Name = "Degang Guo", Address = "Beijing", Phone = "16666666666", }; customerRepository.InsertCustomer(customer2); Console.WriteLine(customer2); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkYellow; List<Customer> allCustomers = customerRepository.GetAllCustomers(); foreach (var customer in allCustomers) { Console.WriteLine(customer); } // =============== 改 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; customer2.Address = "Tianjin"; customerRepository.UpdateCustomer(customer2); Console.WriteLine(customer2); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Cyan; List<Customer> filteredCustomers = customerRepository.GetCustomersByAddress("Tianjin"); foreach (var customer in filteredCustomers) { Console.WriteLine(customer); } // =============== 删 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Blue; customerRepository.DeleteCustomersByAddress("Tianjin"); // =============== 查 =============== Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Magenta; List<Customer> existingCustomers = customerRepository.GetAllCustomers(); foreach (var customer in existingCustomers) { Console.WriteLine(customer); } // =============== 清理 =============== Console.WriteLine(); Console.WriteLine("Press any key to clean database..."); Console.ReadKey(); customerRepository.DeleteAllCustomers(); }