Exemplo n.º 1
0
        private static void AddPerson()
        {
            Console.Write("Add new person(y/n)?");
            if (Console.ReadKey().KeyChar.ToString().ToLower() != "y")
            {
                Console.Clear();
                return;
            }

            Console.Clear();

            var person = new Person();

            Console.Write("Name: ");
            person.Name = Console.ReadLine();

            Console.Write("Age: ");
            person.Age = int.Parse(Console.ReadLine());

            Console.Write("Height: ");
            person.Height = int.Parse(Console.ReadLine());

            using (var db = new ESTest3())
            {
                db.Person.Add(person);
                db.SaveChanges();
            }
        }
Exemplo n.º 2
0
        private static void DBTest()
        {
            using (var db = new ESTest3())
            {
                Console.WriteLine("----------------------------");

                // Get first person in matching set.
                var query = db.Person.Where(p => p.Age > 10 && p.Height < 170);
                var person = query.FirstOrDefault<Person>();
                Console.WriteLine($"First: {(person != null ? person.Name : string.Empty)}.");

                Console.WriteLine("----------------------------");

                // Tryes to get the first person in an empty set.
                query = db.Person.Where(p => p.Age > 10 && p.Height < 20);
                person = query.FirstOrDefault<Person>();
                Console.WriteLine($"Second: {(person != null ? person.Name : string.Empty)}.");

                Console.WriteLine("----------------------------");

                // Gets all persons in matching set as a list (if nothing matches it returns an empty list).
                var person2 = db.Person.Where(p => p.Age > 10 && p.Height < 200).ToList<Person>();

                //Console.WriteLine(person2.GetType().ToString());

                foreach (Person p in person2)
                {
                    Console.WriteLine($"Third: {p.Name}.");
                }

                Console.WriteLine("----------------------------");

                // Gets all persons inn te table.
                foreach (Person p in db.Person)
                {
                    Console.WriteLine($"Forth: {p.Name}.");
                }

                Console.WriteLine("----------------------------");

                // Gets all persons in matching set as a list (if nothing matches it returns an empty DBQuery?).
                var person3 = from p in db.Person
                              where p.Height == 189
                              select p;

                //Console.WriteLine(person3.GetType().ToString());

                foreach (Person p in person3)
                {
                    Console.WriteLine($"Fifth: {p.Name}.");
                }

                Console.WriteLine("----------------------------");
            }
        }