Ejemplo n.º 1
0
 public bool writeToDB()
 {
     Console.WriteLine("\n\nWriting to database...");
     if (this.convertArrayToString())
     {
         using (var db = new SnailContext())
         {
             db.Snails.Add(this);
             db.SaveChanges();
             Console.WriteLine("The result was written successfully!");
             return(true);
         }
     }
     else
     {
         Console.WriteLine("Array is empty!");
         return(false);
     }
 }
Ejemplo n.º 2
0
        public static List <SnailMatrix> readFromDB()
        {
            Console.WriteLine("Reading from database...");
            using (var db = new SnailContext())
            {
                var prevSnails = db.Snails.OrderByDescending(c => c.Id).ToList();
                int counter    = 0;

                if (prevSnails.Any(o => o.Id != null))
                {
                    foreach (var elem in prevSnails)
                    {
                        if (counter++ >= 5)
                        {
                            break;
                        }
                        Console.WriteLine("result #" + elem.Id + ": " + elem.snailedArrayString);
                    }
                }
                else
                {
                    SnailMatrix[] sm = new SnailMatrix[3];
                    for (int i = 0; i < sm.Length; i++)
                    {
                        sm[i] = new SnailMatrix();
                        sm[i].snailedArrayString = "test string for first load";
                    }
                    db.Snails.AddRange(sm);
                    db.SaveChanges();

                    Console.WriteLine("Database is empty!");
                }
                Console.WriteLine();
                return(prevSnails);
            }
            //return false;//for future
        }