예제 #1
0
        public static void Main(string[] args)
        {
            _stopWatch.Start();
            DoEverything().Wait();
            //DoEverythingSync();
            _stopWatch.Stop();
            var elapsedMilliseconds = _stopWatch.ElapsedMilliseconds;
            var elapsedTicks        = _stopWatch.ElapsedTicks;

            Console.WriteLine("Everything took {0} milliseconds and {1} ticks!", elapsedMilliseconds, elapsedTicks);

            _stopWatch.Restart();
            using (var context = EntityFunDbContext.Create())
            {
                var allDogs = context.Dogs
                              .Include(x => x.Friends)
                              .Include(x => x.Owner)
                              .ToList();
                foreach (var dog in allDogs)
                {
                    Debug.WriteLine("{0} is a great dog, his owner is {1} {2} and his best friend is {3}", dog.Name, dog.Owner.Forename, dog.Owner.Surname, dog.Friends.First().Name);
                }
            }
            _stopWatch.Stop();
            elapsedMilliseconds = _stopWatch.ElapsedMilliseconds;
            elapsedTicks        = _stopWatch.ElapsedTicks;
            Console.WriteLine("All stuff took {0} milliseconds and {1} ticks!", elapsedMilliseconds, elapsedTicks);

            _stopWatch.Restart();
            using (var context = EntityFunDbContext.Create())
            {
                var allDogs = context.Dogs
                              .Select(x => new
                {
                    x.Name,
                    Owner = new
                    {
                        Forename = x.Owner.Forename,
                        Surname  = x.Owner.Surname
                    },
                    Friends = x.Friends.Select(y => new
                    {
                        y.Name
                    })
                })
                              .ToList()
                              .Select(x => new Dog
                {
                    Name  = x.Name,
                    Owner = new Human
                    {
                        Forename = x.Owner.Forename,
                        Surname  = x.Owner.Surname
                    },
                    Friends = x.Friends.Select(y => new Dog
                    {
                        Name = y.Name
                    }).ToList()
                });
                foreach (var dog in allDogs)
                {
                    Debug.WriteLine("{0} is a great dog, his owner is {1} {2} and his best friend is {3}", dog.Name, dog.Owner.Forename, dog.Owner.Surname, dog.Friends.First().Name);
                }
            }
            _stopWatch.Stop();
            elapsedMilliseconds = _stopWatch.ElapsedMilliseconds;
            elapsedTicks        = _stopWatch.ElapsedTicks;
            Console.WriteLine("Limited stuff took {0} milliseconds and {1} ticks!", elapsedMilliseconds, elapsedTicks);
            var dogService = new DogService();
            var dog1       = new Dog {
                Id = 1
            };
            var dog2 = new Dog {
                Id = _random.Next(400, 800)
            };

            dogService.MakeFriendAsync(dog1, dog2).Wait();
            using (var context = EntityFunDbContext.Create())
            {
                var test = context.Dogs
                           .Include(x => x.Friends)
                           .First();

                foreach (var friend in test.Friends)
                {
                    Console.WriteLine("{0} - {1}", friend.Id, friend.Name);
                }
            }

            var newDog = dogService.AddDogAsync(new Dog {
                Name = "Brenford", DateOfBirth = DateTime.Now.AddYears(-2)
            });

            Console.ReadKey();
        }