示例#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();
            }
        }
示例#2
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();
        }
示例#3
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();
        }
示例#5
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();
            }
        }
    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();
    }