static void Main()
        {
            var context = new NewsDBContext();

            var data = context.News.Find(1);

            //WARNING: Concurency handling should be tested by running two instances of the app.

            while (true)
            {
                try
                {
                    data.Content = Console.ReadLine();
                    context.SaveChanges();
                    Console.WriteLine("Changes successfully saved in the DB.");
                    break;
                }
                catch (DbUpdateConcurrencyException exc)
                {
                    exc.Entries.Single().Reload();
                    Console.WriteLine("Conflict! Text from DB:" + context.News.Find(1).Content
                        + ". Enter the corrected text:");  
                }
            }         
        }   
        private static void CorrectNewsContent()
        {
            using (var db = new NewsDBContext())
            {
                var news = db.News.ToList();
                var first = news.First();
                Console.WriteLine("Text from DB: {0}", first.Content);

                Console.WriteLine("Enter the corrected text: ");
                string input = Console.ReadLine();
                first.Content = input;

                try
                {
                    db.SaveChanges();

                    Console.WriteLine("Changes successfully saved in the DB.");
                }
                catch (DbUpdateConcurrencyException)
                {
                    Console.WriteLine("Conflict!");
                    CorrectNewsContent();
                }
            }
        }
        private static void ConcurrencyFirstWins()
        {
            // The first user changes some record
            var contextFirstUser = new NewsDBContext();
            var newsFirstUser = contextFirstUser.Newses.Find(1);
            Console.WriteLine("Text from DB: " + newsFirstUser.Content);
            Console.Write("Enter the corrected text: ");
            string newValueFirstUser = Console.ReadLine();
            newsFirstUser.Content = newValueFirstUser;

            // The second user changes the same record
            var contextSecondUser = new NewsDBContext();
            var newsSecondUser = contextSecondUser.Newses.Find(1);
            Console.WriteLine("Text from DB: " + newsSecondUser.Content);
            Console.Write("Enter the corrected text: ");
            string newValueSecondUser = Console.ReadLine();
            newsSecondUser.Content = newValueSecondUser;

            // Conflicting changes: first wins; second gets an exception
            contextFirstUser.SaveChanges();
            try
            {
                contextSecondUser.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                Console.WriteLine("Conflict! Text from DB: " + newValueFirstUser);
                Console.WriteLine(ex.Message);
            }
        }
        static void Main()
        {
            var contextFirst = new NewsDBContext();
            var lastNewFirstUer =
                contextFirst.News.OrderByDescending(n => n.Id).First();

            var contextSecondUser = new NewsDBContext();
            var lastNewSecondUser =
                contextSecondUser.News.OrderByDescending(n => n.Id).First();

            Console.WriteLine("Application started.");

            Console.WriteLine();
            
            Console.WriteLine("First User.");
            Console.WriteLine("Text from DB: " + lastNewFirstUer.ContentNews);
            Console.Write("Enter the corrected text: ");
            
            string firstUserInput = Console.ReadLine();
            lastNewFirstUer.ContentNews = firstUserInput;

            contextFirst.SaveChanges();
            Console.WriteLine("Changes successfully saved in the DB.");

            Console.WriteLine("Second User.");
            Console.WriteLine("Text from DB: " + lastNewFirstUer.ContentNews);
            Console.Write("Enter the corrected text: ");

            string secondUserInput = Console.ReadLine();
            lastNewSecondUser.ContentNews = secondUserInput;

            try
            {
                contextSecondUser.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                Console.WriteLine("Conflict! Text from DB: " + lastNewFirstUer.ContentNews);
                Console.WriteLine("Enter the corrected text: ");

                string textAfterConflict = Console.ReadLine();

                var newContext = new NewsDBContext();
                var currNew =
                contextSecondUser.News.OrderByDescending(n => n.Id).First();
                currNew.ContentNews = textAfterConflict;
            }
            Console.WriteLine("Changes successfully saved in the DB.");
               
        }
        public static void ConcurrentUpdates()
        {
            // Problem 2.Concurrent Updates (Console App) -

            // Step 1.At startup, the app should load from the DB the news text and print it on the console.
            var firstContext = new NewsDBContext();
            var firstConcurrentNews = firstContext.NewsItems.Find(1);
            Console.WriteLine("Text from DB: " + firstConcurrentNews.Content + "\n");

            // Step 2.After that, the app should enter a new value for the news text from the console.
            Console.Write("User-1, Enter the corrected text: ");
            firstConcurrentNews.Content = Console.ReadLine();
            Console.WriteLine();

            var secondContext = new NewsDBContext();
            var secondConcurrentNews = secondContext.NewsItems.Find(1);
            Console.Write("User-2, Enter the corrected text: ");
            secondConcurrentNews.Content = Console.ReadLine();
            Console.WriteLine();

            // Step 3.After entering a new value, the app should try to save it to the DB.
            // In case of success (no conflicting updates), the app should say
            // that the changes were saved and should finish its work.
            // In case of concurrent update conflict, the app should display
            // an error message, should display the new (changed) text from the DB and should go to Step 2.
            firstContext.SaveChanges(); // first user

            Console.WriteLine(("Changes successfully saved in the DB.\n").ToUpper());
            Console.WriteLine("Text from DB after first changes: " +
                firstConcurrentNews.Content);
            try
            {
                secondContext.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                Console.WriteLine("Concurrent Update Conflict!" + ex.Message);
                Console.WriteLine("Text from DB: " +
                    firstConcurrentNews.Content +
                    "\n\nUser-2, Enter the corrected text:");
                firstConcurrentNews.Content = Console.ReadLine();
                Console.WriteLine(("\nChanges successfully saved in the DB.\n").ToUpper());
                Console.WriteLine("Text from DB after second changes: " +
                firstConcurrentNews.Content);
            }
            firstContext.SaveChanges();
        }
        private static void OptimisticConcurrencyLastWins()
        {
            // The first user changes some record
            var contextFirstUser = new NewsDBContext();
            var newsFirstUser = contextFirstUser.Newses.Find(1);
            Console.WriteLine("Text from DB: " + newsFirstUser.Content);
            Console.Write("Enter the corrected text: ");
            string newValueFirstUser = Console.ReadLine();
            newsFirstUser.Content = newValueFirstUser;

            // The second user changes the same record
            var contextSecondUser = new NewsDBContext();
            var newsSecondUser = contextSecondUser.Newses.Find(1);
            Console.WriteLine("Text from DB: " + newsSecondUser.Content);
            Console.Write("Enter the corrected text: ");
            string newValueSecondUser = Console.ReadLine();
            newsSecondUser.Content = newValueSecondUser;

            // Conflicting changes: last wins
            contextFirstUser.SaveChanges();
            contextSecondUser.SaveChanges();
            Console.WriteLine("Changes successfully saved in the DB.");
        }
        static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<NewsDBContext, Configuration>());

            var context = new NewsDBContext();

            //create database
            var count = context.Newses.Count();
            Console.WriteLine(count);

            //Problem 2. Concurrent Updates
            //Write a console app that edits the first news item in the DB. It should detect and handle any concurrent updates.
                //Step 1.   At startup, the app should load from the DB the news text and print it on the console.
                //Step 2.	After that, the app should enter a new value for the news text from the console.
                //Step 3.	After entering a new value, the app should try to save it to the DB.
                    //o	In case of success (no conflicting updates), the app should say that the changes were saved and should finish its work.
                    //o	In case of concurrent update conflict, the app should display an error message, should display the new (changed) text from the DB and should go to Step 2.
            //Run simultaneously two instances of your app to simulate two concurrent users. Make an update conflict in the database and handle it.

            //OptimisticConcurrencyLastWins();

            ConcurrencyFirstWins();
        }
        private void Edit_OnLoaded(object sender, RoutedEventArgs e)
        {
            Context = new NewsDBContext();

            Data = Context.News.Find(1);
        }