コード例 #1
0
        private static void ConflictingUpdateLastWins()
        {
            NorthwindEntities context = new NorthwindEntities();
            Category newCategory = new Category()
            {
                CategoryName = "New Category",
                Description = "New category, just for testing"
            };
            context.Categories.AddObject(newCategory);
            context.SaveChanges();

            // This context works in different transaction
            NorthwindEntities anotherContext = new NorthwindEntities();

            Category lastCategory =
                (from cat in anotherContext.Categories
                 where cat.CategoryID == newCategory.CategoryID
                 select cat).First();
            lastCategory.CategoryName = lastCategory.CategoryName + " 2";
            anotherContext.SaveChanges();

            // This will cause OptimisticConcurrencyException if
            // Categories.CategoryName has ConcurrencyMode=Fixed
            newCategory.CategoryName = newCategory.CategoryName + " 3";
            context.SaveChanges();
        }
コード例 #2
0
        private static void ConflictingUpdateLastWins()
        {
            NorthwindEntities context     = new NorthwindEntities();
            Category          newCategory = new Category()
            {
                CategoryName = "New Category",
                Description  = "New category, just for testing"
            };

            context.Categories.AddObject(newCategory);
            context.SaveChanges();

            // This context works in different transaction
            NorthwindEntities anotherContext = new NorthwindEntities();

            Category lastCategory =
                (from cat in anotherContext.Categories
                 where cat.CategoryID == newCategory.CategoryID
                 select cat).First();

            lastCategory.CategoryName = lastCategory.CategoryName + " 2";
            anotherContext.SaveChanges();

            // This will cause OptimisticConcurrencyException if
            // Categories.CategoryName has ConcurrencyMode=Fixed
            newCategory.CategoryName = newCategory.CategoryName + " 3";

            // Uncomment for testing
            //try
            //{
            context.SaveChanges();
            //}
            //catch
            //{
            //    context.Refresh(RefreshMode.ClientWins, newCategory);
            //    context.SaveChanges();
            //}
        }
コード例 #3
0
        private static void AddCategoriesInTransaction()
        {
            NorthwindEntities context = new NorthwindEntities();

            // Add a valid new category
            Category newCategory = new Category()
            {
                CategoryName = "New Category",
                Description = "New category, just for testing"
            };
            context.Categories.AddObject(newCategory);

            // Add an invalid new category
            Category newCategoryLongName = new Category()
            {
                CategoryName = "New Category Loooooooong Name",
            };
            context.Categories.AddObject(newCategoryLongName);

            // The entire transaction will fail due to
            // insertion failure for the second category
            context.SaveChanges();
        }