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(); }
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(); }
/// <summary> /// Create a new Category object. /// </summary> /// <param name="categoryID">Initial value of the CategoryID property.</param> /// <param name="categoryName">Initial value of the CategoryName property.</param> public static Category CreateCategory(global::System.Int32 categoryID, global::System.String categoryName) { Category category = new Category(); category.CategoryID = categoryID; category.CategoryName = categoryName; return category; }
/// <summary> /// Deprecated Method for adding a new object to the Categories EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToCategories(Category category) { base.AddObject("Categories", category); }