static void Main(string[] args) { var db = new NewsContext(); bool hasChange = false; var firstUser = new NewsContext(); var secondUser = new NewsContext(); int id = 1; ChangeContentValue(firstUser,id,ref hasChange); Console.WriteLine(hasChange); ChangeContentValue(secondUser, id,ref hasChange); firstUser.SaveChanges(); try { secondUser.SaveChanges(); } catch (DbUpdateConcurrencyException ) { Console.WriteLine("Conflict! Text from DB: "); PrintNewsContent(id); ChangeContentValue(secondUser,id,ref hasChange); Console.WriteLine("Changes successfully saved in the DB."); } }
static void UpdateEntry() { using (var db = new NewsContext()) { var news = db.News.FirstOrDefault(); bool updateFailed; do { updateFailed = false; try { Console.Write("Enter updated news content: "); var updatedEntry = Console.ReadLine(); news.Content = updatedEntry; db.SaveChanges(); Console.WriteLine("---Entry updated successfully.---\n"); } catch (DbUpdateConcurrencyException) { updateFailed = true; Console.WriteLine("---Concurrency conflict. Try again.---"); } } while (updateFailed); } }
private static void ReadAndEditFirstNews() { var db1 = new NewsContext(); var firstNews = db1.News.First(); firstNews.Content = Console.ReadLine(); var db2 = new NewsContext(); var secondNews = db2.News.First(); secondNews.Content = Console.ReadLine(); Console.WriteLine("First news content:"); Console.WriteLine(firstNews.Content); Console.WriteLine("Second news content:"); Console.WriteLine(secondNews.Content); db1.SaveChanges(); try { db2.SaveChanges(); Console.WriteLine("Changes successfully saved in the DB."); } catch (DbUpdateConcurrencyException) { Console.WriteLine("Conflict! The content is already changed."); } }