static void RunExample() { int postId = 0; using (var context = new EFRecipesEntities()) { // post is created var post = new ForumPost { ForumUser = "******", IsActive = false, Post = "The moderator is a great guy." }; context.ForumPosts.AddObject(post); context.SaveChanges(); postId = post.PostingId; } using (var context = new EFRecipesEntities()) { // moderator gets post to review var post = context.ForumPosts.First(p => p.PostingId == postId); Console.WriteLine("Post by {0}: {1}", post.ForumUser, post.Post); // poster changes post out-of-band context.ExecuteStoreCommand(@"update chapter14.forumpost set post='The moderator''s mom dresses him funny.' where postingId = @p0", new object[] { postId.ToString() }); Console.WriteLine("Fast Eddie changes the post"); // moderator doesn't trust Fast Eddie if (string.Compare(post.ForumUser, "FastEddie27") == 0) { post.IsActive = false; } else { post.IsActive = true; } try { // refresh any changes to the TimeStamp context.ForumPosts.MergeOption = MergeOption.PreserveChanges; post = context.ForumPosts.First(p => p.PostingId == postId); context.SaveChanges(); Console.WriteLine("No concurrency exception."); } catch (OptimisticConcurrencyException) { try { context.Refresh(RefreshMode.ClientWins, post); context.SaveChanges(); } catch (OptimisticConcurrencyException) { // we tried twice...do something else } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
/// <summary> /// Deprecated Method for adding a new object to the ForumPosts EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToForumPosts(ForumPost forumPost) { base.AddObject("ForumPosts", forumPost); }