Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        static void Main()
        {
            var db = new NewsContext();

            UpdateEntry();
            PrintEntries(db);
        }
 private static void PrintNewsContent(int id)
 {
     var db = new NewsContext();
     var newsToChange = db.News.Find(id);
     Console.WriteLine(newsToChange.Content);
     
 }
        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.");
            }

        }
Esempio n. 5
0
        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.");
            }
        }
Esempio n. 6
0
        static void PrintEntries(NewsContext db)
        {
            var news = db.News
                .Select(n => n.Content);

            foreach (var item in news)
            {
                Console.WriteLine(item);
            }
        }
 private static void ChangeContentValue(NewsContext db, int id, ref bool hasChange)
 {
     var article = db.News.Find(id).Content;
     Console.WriteLine("Please enter content : ");
     var newContent = Console.ReadLine();
     db.News.Find(id).Content = newContent;
     if (hasChange == false)
     {
         Console.WriteLine("Changes successfully saved in the DB.");
     }
     hasChange = true;
 }