Пример #1
0
        static void TestVersionControl()
        {
            using (NorthwindEntities NWEntities = new NorthwindEntities())
            {
                // first user
                Console.WriteLine("First User ...");
                Product product = (from p in NWEntities.Products
                                   where p.ProductID == 3
                                   select p).First();
                Console.WriteLine("Original unit in stock: {0}",
                                  product.UnitsInStock);
                product.UnitsInStock += 1;
                Console.WriteLine("Current unit in stock to update: {0}",
                                  product.UnitsInStock);
                // process more products

                // second user
                Console.WriteLine("\nSecond User ...");
                using (NorthwindEntities1 NWEntities1 = new NorthwindEntities1())
                {
                    Product1 product1 = (from p in NWEntities1.Product1s
                                         where p.ProductID == 3
                                         select p).First();
                    Console.WriteLine("Original unit in stock: {0}",
                                      product1.UnitsInStock);
                    product1.UnitsInStock += 2;
                    Console.WriteLine("Current unit in stock to update: {0}",
                                      product1.UnitsInStock);
                    NWEntities1.SaveChanges();
                    Console.WriteLine("update submitted to database");
                }

                // first user is ready to submit changes
                Console.WriteLine("\nFirst User ...");
                try
                {
                    NWEntities.SaveChanges();
                }
                catch (DbUpdateConcurrencyException e)
                {
                    Console.WriteLine("Conflicts detected. Refreshing ...");
                    var entry = e.Entries.Single();
                    entry.OriginalValues.SetValues(entry.GetDatabaseValues());
                    NWEntities.SaveChanges();
                    Console.WriteLine("update submitted to database after refresh");
                }
            }
        }
Пример #2
0
        static void TestVersionControl()
        {
            using (NorthwindEntities NWEntities = new NorthwindEntities())
            {

                // first user
                Console.WriteLine("First User ...");
                Product product = (from p in NWEntities.Products
                                   where p.ProductID == 3
                                   select p).First();
                Console.WriteLine("Original unit in stock: {0}",
                                product.UnitsInStock);
                product.UnitsInStock += 1;
                Console.WriteLine("Current unit in stock to update: {0}",
                    product.UnitsInStock);
                // process more products

                // second user
                Console.WriteLine("\nSecond User ...");
                using (NorthwindEntities1 NWEntities1 = new NorthwindEntities1())
                {
                    Product1 product1 = (from p in NWEntities1.Product1s
                                         where p.ProductID == 3
                                         select p).First();
                    Console.WriteLine("Original unit in stock: {0}",
                        product1.UnitsInStock);
                    product1.UnitsInStock += 2;
                    Console.WriteLine("Current unit in stock to update: {0}",
                        product1.UnitsInStock);
                    NWEntities1.SaveChanges();
                    Console.WriteLine("update submitted to database");
                }

                // first user is ready to submit changes
                Console.WriteLine("\nFirst User ...");
                try
                {
                    NWEntities.SaveChanges();
                }
                catch (DbUpdateConcurrencyException e)
                {
                    Console.WriteLine("Conflicts detected. Refreshing ...");
                    var entry = e.Entries.Single();
                    entry.OriginalValues.SetValues(entry.GetDatabaseValues());
                    NWEntities.SaveChanges();
                    Console.WriteLine("update submitted to database after refresh");
                }
            }
        }