Exemplo n.º 1
0
        public static void Main()
        {
            using (var firstDbContext = new NorthwindEntities())
            {
                var firstEmployeeFromFirstDbContext = firstDbContext.Employees.First();
                Console.WriteLine("First user see: {0}", firstEmployeeFromFirstDbContext.FirstName);
                firstEmployeeFromFirstDbContext.FirstName = "First";
                Console.WriteLine("First user changes the name with new value: {0}\n", firstEmployeeFromFirstDbContext.FirstName);

                firstDbContext.SaveChanges();

                using (var secondDbContext = new NorthwindEntities())
                {
                    var firstEmployeeFromSecondDbContext = secondDbContext.Employees.First();

                    Console.WriteLine("Second user see: {0}", firstEmployeeFromSecondDbContext.FirstName);
                    firstEmployeeFromSecondDbContext.FirstName = "Second";
                    Console.WriteLine("Second user changes the name with new value: {0}\n", firstEmployeeFromSecondDbContext.FirstName);

                    secondDbContext.SaveChanges();
                }

                Console.WriteLine("After all changes:");
                Console.WriteLine("First user see: {0}\n", firstEmployeeFromFirstDbContext.FirstName);
            }

            using (var northwindEntities = new NorthwindEntities())
            {
                Console.WriteLine("Actual result: {0}", northwindEntities.Employees.First().FirstName);
            }
        }
 private static void StartSecondConnection(NorthwindEntities firstConnection)
 {
     using (var secondConnection = new NorthwindEntities())
     {
         PrintWhatSecondUserSees(secondConnection);
         firstConnection.SaveChanges();
         secondConnection.SaveChanges();
         PrintWhatSecondUserSeesAfterChanges(secondConnection);
     }
 }
        public static int InsertCustomer(Customer customer)
        {
            var affectedRows = 0;
            using (var db = new NorthwindEntities())
            {
                db.Customers.Add(customer);
                affectedRows = db.SaveChanges();
            }

            return affectedRows;
        }
Exemplo n.º 4
0
        public static int DeleteCustomer(string customerId)
        {
            using (var dbContext = new NorthwindEntities())
            {
                var customer = dbContext.Customers.Find(customerId);
                dbContext.Customers.Remove(customer);
                int affectedRows = dbContext.SaveChanges();

                return affectedRows;
            }
        }
Exemplo n.º 5
0
        public static int ModifyCustomerCompanyName(string customerId, string newCompanyName)
        {
            using (var dbContext = new NorthwindEntities())
            {
                var customer = dbContext.Customers.Find(customerId);
                customer.CompanyName = newCompanyName;
                int affectedRows = dbContext.SaveChanges();

                return affectedRows;
            }
        }
        public static int ModifyCustomerAddress(string customerID, string newAddress)
        {
            var affectedRows = 0;
            using (var db = new NorthwindEntities())
            {
                var targetCustomer = db.Customers.Find(customerID);
                targetCustomer.Address = newAddress;
                affectedRows = db.SaveChanges();
            }

            return affectedRows;
        }
        private static int TestWithTransaction()
        {
            var affectedRows = 0;
            var customerId = "RATTC";
            var employeeId = 5;

            var invalidEmployeeId = 5000;

            using (var dbContext = new NorthwindEntities())
            {
                using (var transaction = dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        // To test with invalid data and see what happens, change employeeId with invalidEmployeeId
                        // then the transaction will rollback and there will be no added orders
                        var firstOrder = new Order()
                        {
                            CustomerID = customerId,
                            EmployeeID = employeeId
                        };

                        dbContext.Orders.Add(firstOrder);

                        var secondOrder = new Order()
                        {
                            CustomerID = customerId,
                            EmployeeID = employeeId
                        };

                        dbContext.Orders.Add(secondOrder);

                        affectedRows = dbContext.SaveChanges();

                        transaction.Commit();

                        Console.WriteLine("- Finish successfully => Commit transaction");
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();

                        Console.WriteLine("- Exception: Finish Unsuccessfully => Rollback transaction");
                    }
                }
            }

            return affectedRows;
        }
        public static int DeleteCustomerById(string customerID)
        {
            var affectedRows = 0;
            using (var db = new NorthwindEntities())
            {
                Customer customerToDelete = db.Customers.Find(customerID);
                if (customerToDelete != null)
                {
                    db.Customers.Remove(customerToDelete);
                    affectedRows = db.SaveChanges();
                }
            }

            return affectedRows;
        }
        public static int DeleteCustomer(string customerId)
        {
            var affectedRows = 0;

            using (var dbContext = new NorthwindEntities())
            {
                var customersToDelete = dbContext.Customers.Where(c => c.CustomerID == customerId);

                if (customersToDelete.Count() > 0)
                {
                    foreach (var customer in customersToDelete)
                    {
                        dbContext.Customers.Remove(customer);
                    }
                }

                affectedRows = dbContext.SaveChanges();
            }

            return affectedRows;
        }
Exemplo n.º 10
0
        public static int InsertCustomer(
            string customerID,
            string companyName,
            string contactName = null,
            string city = null,
            string contactTitle = null,
            string address = null,
            string region = null,
            string postalCode = null,
            string country = null,
            string phone = null,
            string fax = null)
        {
            var newCustomer = new Customer
            {
                CustomerID = customerID,
                CompanyName = companyName,
                City = city,
                ContactName = contactName,
                ContactTitle = contactTitle,
                Address = address,
                Region = region,
                PostalCode = postalCode,
                Country = country,
                Phone = phone,
                Fax = fax,
            };

            using (var dbContext = new NorthwindEntities())
            {
                dbContext.Customers.Add(newCustomer);
                int affectedRows = dbContext.SaveChanges();

                return affectedRows;
            }
        }
        /// <summary>
        /// Started the transaction explicitly. We have options to commit / roll-back transactions.
        /// </summary>
        private static int TestOnExplicitlyStartedTransaction()
        {
            var affectedRows = 0;
            var customerId = "VINET";
            var employeeId = 5;

            var invalidEmployeeId = int.MaxValue;

            using (var dbContext = new NorthwindEntities())
            {
                using (var transaction = dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        #region [Add Orders]
                        
                        // This cause an error
                        var firstOrder = new Order()
                        {
                            CustomerID = customerId,
                            EmployeeID = invalidEmployeeId // employeeId
                        };
                        
                        dbContext.Orders.Add(firstOrder);
                        
                        var secondOrder = new Order()
                        {
                            CustomerID = customerId,
                            EmployeeID = employeeId
                        };
                        
                        secondOrder.Order_Details.Add(new Order_Detail()
                        {
                            OrderID = secondOrder.OrderID,
                            ProductID = 5,
                            UnitPrice = 12.34m,
                            Quantity = 100,
                            Discount = 0.2f
                        });
                        
                        dbContext.Orders.Add(secondOrder);
                        
                        #endregion
                        
                        affectedRows = dbContext.SaveChanges();
                        
                        // Finish successfully => Commit transaction
                        transaction.Commit();
                        
                        // Test to Rollback() insted of Commit() and you will see that the changes are rolled-back
                        //transaction.Rollback();
                        
                        Console.WriteLine("- Finish successfully => Commit transaction");
                    }
                    catch (Exception)
                    {
                        // Finish Unsuccessfully => Rollback transaction
                        transaction.Rollback();
                        
                        Console.WriteLine("- Exception: Finish Unsuccessfully => Rollback transaction");
                    }
                }
            }
            
            return affectedRows;
        }
        /// <summary>
        /// Started the transaction implicitly. 
        /// </summary>
        private static int TestOnImplicitlyStartedTransaction()
        {
            var affectedRows = 0;
            var customerId = "RATTC";
            var employeeId = 6;
            
            var invalidEmployeeId = int.MaxValue;
            
            using (var dbContext = new NorthwindEntities())
            {
                try
                {
                    #region [Add Orders]
                    
                    // This cause an error
                    var firstOrder = new Order()
                    {
                        CustomerID = customerId,
                        EmployeeID = invalidEmployeeId // employeeId
                    };
                    
                    dbContext.Orders.Add(firstOrder);
                    
                    var secondOrder = new Order()
                    {
                        CustomerID = customerId,
                        EmployeeID = employeeId
                    };
                    
                    secondOrder.Order_Details.Add(new Order_Detail()
                    {
                        OrderID = secondOrder.OrderID,
                        ProductID = 5,
                        UnitPrice = 12.34m,
                        Quantity = 100,
                        Discount = 0.2f
                    });

                    dbContext.Orders.Add(secondOrder);

                    #endregion
                
                    affectedRows = dbContext.SaveChanges();
                
                    Console.WriteLine("- Finish successfully => Commit transaction");
                }
                catch (Exception)
                {
                    Console.WriteLine("- Exception: Finish Unsuccessfully => Rollback transaction");
                }
            }
        
            return affectedRows;
        }