Ejemplo n.º 1
0
        protected override void Seed(MountainsContext context)
        {
            if (!context.Countries.Any())
            {
                Country bulgaria = new Country()
                {
                    Code = "BG",
                    Name = "Bulgaria"
                };

                context.Countries.Add(bulgaria);
                context.SaveChanges();

                Mountain rila = new Mountain()
                {
                    Name = "Rila",
                };

                context.Mountains.Add(rila);
                context.SaveChanges();

                bulgaria.Mountains.Add(rila);

                Peak musala = new Peak()
                {
                    Name       = "Musala",
                    MountainId = rila.Id,
                    Elevation  = 2925,
                };

                context.Peaks.Add(musala);

                context.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            var context   = new MountainsContext();
            var mountains = context.Mountains
                            .Select(m => new
            {
                m.Name,
                Countries = m.Countries.Select(c => c.CountryName),
                Peaks     = m.Peaks.Select(p => new
                {
                    p.Name,
                    p.Elevation
                })
            })
                            .ToList();

            mountains.ForEach(m =>
            {
                var countries = m.Countries.ToList();
                Console.WriteLine("{0} in {1} with peaks:", m.Name,
                                  string.Join(", ", countries));
                m.Peaks.ToList().ForEach(p =>
                {
                    Console.WriteLine(" {0} : {1} elevation", p.Name, p.Elevation);
                });
            });
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            var context = new MountainsContext();
            var mountains = context.Mountains
                .Select(m => new
                {
                    m.Name,
                    Countries = m.Countries.Select(c => c.CountryName),
                    Peaks = m.Peaks.Select(p => new
                    {
                        p.Name,
                        p.Elevation
                    })
                })
                .ToList();

            mountains.ForEach(m =>
            {
                var countries = m.Countries.ToList();
                Console.WriteLine("{0} in {1} with peaks:", m.Name,
                    string.Join(", ", countries));
                m.Peaks.ToList().ForEach(p =>
                {
                    Console.WriteLine(" {0} : {1} elevation", p.Name, p.Elevation);
                });
            });
        }
Ejemplo n.º 4
0
 public static void Main()
 {
     using (var context = new MountainsContext())
     {
         var mountains = context.Mountains.Include(p => p.Peaks).Include(c => c.Countries).ToList();
         foreach (var mountain in mountains)
         {
             Console.Write(mountain.Name);
             Console.Write("; Peaks: " + string.Join(", ", mountain.Peaks.Select(m => m.Name)));
             Console.WriteLine("; Countirues: " + string.Join(", ", mountain.Countries.Select(m => m.Name)));
         }
     }
 }
Ejemplo n.º 5
0
        private static void ImportMountain(MountainDTO mountainDTO)
        {
            var context = new MountainsContext();

            // Parse mountain
            if (mountainDTO.MountainName == null)
            {
                throw new Exception("Missing mountain name");
            }
            var mountainToImport = new Mountain {
                Name = mountainDTO.MountainName
            };

            // Parse peaks optional
            foreach (var peakDto in mountainDTO.Peaks)
            {
                if (peakDto.PeakName == null)
                {
                    throw new Exception("Missing peak name");
                }
                if (peakDto.Elevation == null)
                {
                    throw new Exception("Missing peak elevation");
                }

                mountainToImport.Peaks.Add(new Peak()
                {
                    Name      = peakDto.PeakName,
                    Elevation = peakDto.Elevation.GetValueOrDefault()
                });
            }

            // Parse countries optional
            foreach (var countryName in mountainDTO.Countries)
            {
                var country = context.Countries.FirstOrDefault(c => c.Name == countryName);
                if (country == null)
                {
                    country = new Country()
                    {
                        Code = countryName.ToUpper().Substring(0, 2),
                        Name = countryName
                    };
                }

                mountainToImport.Countries.Add(country);
            }

            context.Mountains.Add(mountainToImport);
            context.SaveChanges();
        }
Ejemplo n.º 6
0
        private static void ImportMountain(MountainJson mountain)
        {
            var context = new MountainsContext();

            if (string.IsNullOrEmpty(mountain.MountainName))
            {
                throw new ArgumentException("Mountain name is required.");
            }

            var peaks = new List <Peak>();

            foreach (var peak in mountain.Peaks)
            {
                if (string.IsNullOrEmpty(peak.PeakName))
                {
                    throw new ArgumentException("Peak name is required.");
                }

                if (peak.Elevation == null)
                {
                    throw new ArgumentException("Peak elevation is required.");
                }

                var dataPeak = new Peak()
                {
                    Name      = peak.PeakName,
                    Elevation = (int)peak.Elevation
                };

                peaks.Add(dataPeak);
            }

            var countries = mountain.Countries
                            .Select(country => new Country()
            {
                CountryName = country,
                CountryCode = country.Substring(0, 2).ToUpper()
            })
                            .ToList();

            var dataMountain = new Mountain()
            {
                Name      = mountain.MountainName,
                Countries = countries,
                Peaks     = peaks
            };

            context.Mountains.AddOrUpdate(dataMountain);
            context.SaveChanges();
        }
        private static void AddCountryToDB(MountainDTO mountainDTO)
        {
            var context = new MountainsContext();

            if (mountainDTO.MountainName == null)
            {
                throw new Exception("Mountain name is required");
            }
            var m = new Mountain()
            {
                Name = mountainDTO.MountainName
            };

            foreach (var peakDTO in mountainDTO.Peaks)
            {
                if (peakDTO.PeakName == null)
                {
                    throw new Exception("Peak name is required");
                }
                if (peakDTO.Elevation == null)
                {
                    throw new Exception("Peak elevation is required");
                }
                Peak peak = new Peak();
                peak.Name      = peakDTO.PeakName;
                peak.Elevation = peakDTO.Elevation.GetValueOrDefault();
                m.Peaks.Add(peak);
            }
            foreach (var countryName in mountainDTO.Countries)
            {
                var country = context.Countries.FirstOrDefault(c => c.Name == countryName);
                if (country == null)
                {
                    country = new Country()
                    {
                        Code = countryName.ToUpper().Substring(0, 2),
                        Name = countryName
                    };
                }
                m.Countries.Add(country);
            }
            context.Mountains.Add(m);
            context.SaveChanges();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            var context = new MountainsContext();

            var json = File.ReadAllText(@"..\..\mountains.json");
            JavaScriptSerializer ser = new JavaScriptSerializer();
            var mountains            = ser.Deserialize <MountainsDto[]>(json);

            foreach (var mountainsDto in mountains)
            {
                var mountain = new Mountain();
                mountain.Name = mountainsDto.MountainName;

                context.Mountains.Add(mountain);

                foreach (var peakDto in mountainsDto.Peaks)
                {
                    var peak = new Peak();
                    peak.Name      = peakDto.PeakName;
                    peak.Elevation = peakDto.Elevation;

                    mountain.Peaks.Add(peak);
                }

                foreach (var countryNameDto in mountainsDto.Countries)
                {
                    var country = context.Countries.FirstOrDefault(c => c.Name == countryNameDto);

                    if (country == null)
                    {
                        country = new Country()
                        {
                            Code = new String(countryNameDto.ToUpper().Take(2).ToArray()),
                            Name = countryNameDto
                        };
                    }

                    mountain.Countries.Add(country);
                }

                context.SaveChanges();
            }
        }
Ejemplo n.º 9
0
        internal static void Main(string[] args)
        {
            var context = new MountainsContext();
            var mountains = context.Mountains.Select(m =>
                new
                {
                    m.Name, 
                    Countries = m.Countries.Select(c => c.Name), 
                    Peaks = m.Peaks.Select(p => new { p.Name, p.Elevation })
                });

            foreach (var mountain in mountains)
            {
                Console.WriteLine(
                    "{0}, countries: {1}, peaks: {2}", 
                    mountain.Name, 
                    string.Join(", ", mountain.Countries), 
                    string.Join(", ", mountain.Peaks.Select(
                        p => p.Name + " (elevation " + p.Elevation + ")")));
            }
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            var context = new MountainsContext();

            //6.ListMountainsAndTheirCountriesAndPeaks
            //var mountains = context.Mountains
            //    .Select(m => new
            //    {
            //        m.Name,
            //        Countries = m.Countries
            //            .Select(c => c.CountryName),
            //        Peaks = m.Peaks
            //            .Select(p => p.Name)
            //    });

            //foreach (var mountain in mountains)
            //{
            //    Console.WriteLine("Mountain: {0}\nCountries: {1}\nPeaks: {2}", mountain.Name, string.Join(", ", mountain.Countries),
            //        string.Join(", ", mountain.Peaks));
            //}
        }
    private static void ImportMountain(JToken mountainObj)
    {
        var context  = new MountainsContext();
        var mountain = new Mountain();

        // Parse mountain
        if (mountainObj["mountainName"] == null)
        {
            throw new Exception("Missing mountain name");
        }
        mountain.Name = mountainObj["mountainName"].Value <string>();

        // Parse peaks
        var peaks = mountainObj["peaks"];

        if (peaks != null)
        {
            foreach (var peak in peaks)
            {
                if (peak["peakName"] == null)
                {
                    throw new Exception("Missing peak name");
                }
                string peakName = peak["peakName"].Value <string>();

                if (peak["elevation"] == null)
                {
                    throw new Exception("Missing peak elevation");
                }
                int elevation = peak["elevation"].Value <int>();

                mountain.Peaks.Add(new Peak()
                {
                    Name = peakName, Elevation = elevation
                });
            }
        }

        // Parse countries
        var countryNames = mountainObj["countries"];

        if (countryNames != null)
        {
            foreach (var countryName in countryNames)
            {
                var countryCode =
                    new string(countryName.Value <string>().Take(2).ToArray()).ToUpper();
                var country = context.Countries.Find(countryCode);
                if (country == null)
                {
                    country = new Country()
                    {
                        Code = countryCode, Name = countryName.Value <string>()
                    };
                }
                mountain.Countries.Add(country);
            }
        }

        // Persist the mountain in the database
        context.Mountains.Add(mountain);
        context.SaveChanges();
    }