예제 #1
0
        static void Main(string[] args)
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion <MountainsContext, Configuration>());
            var context = new MountainsContext();

            Country c = new Country()
            {
                Code = "AB", Name = "Balgaria"
            };
            Mountain m = new Mountain()
            {
                Name = "Stara Planina"
            };

            m.Peaks.Add(new Peak()
            {
                Name = "Musala", Mountain = m
            });
            m.Peaks.Add(new Peak()
            {
                Name = "Vihren", Mountain = m
            });
            c.Mountains.Add(m);

            context.Countries.Add(c);
            context.SaveChanges();
        }
예제 #2
0
        private static void Main()
        {
            // pres Enable-Migrations
            //  Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemDbContext, Configuration>());
            //db.Database.Initialize(true);


            var context = new MountainsContext();

            var bulgaria = new Country {
                Code = "1S", Name = "Bulagria"
            };
            var rila = new Mountain {
                Name = "Rila", Countries = { bulgaria }
            };
            var musala = new Peak {
                Name = "Musala", Mountain = rila, Elevation = 2925
            };

            context.Countries.Add(bulgaria);
            context.Mountains.Add(rila);
            context.Peaks.Add(musala);
            context.SaveChanges();

            var countriesQuery = context.Countries.Select(c =>
                                                          new
            {
                ContryName = c.Name,
                Mountains  = c.Mountains.Select(m =>
                                                new
                {
                    m.Name,
                    m.Peaks
                })
            });


            foreach (var country in countriesQuery)
            {
                Console.WriteLine("Country: " + country.ContryName);
                foreach (var mountain in country.Mountains)
                {
                    Console.WriteLine("     Mountain: " + mountain.Name);
                    foreach (var peak in mountain.Peaks)
                    {
                        Console.WriteLine("\t{0} ({1})", peak.Name, peak.Elevation);
                    }
                }
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            var context   = new MountainsContext();
            var countries = context.Countries.Count();

            var newCountry = new Country()
            {
                Name = "ZZ Country",
                Code = "ZZ"
            };

            context.Countries.Add(newCountry);
            context.SaveChanges();
            //Console.WriteLine(countries);
        }
        private static void Main()
        {
            // pres Enable-Migrations
            //  Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemDbContext, Configuration>());
            //db.Database.Initialize(true);

            var context = new MountainsContext();

            var bulgaria = new Country { Code = "1S", Name = "Bulagria" };
            var rila = new Mountain { Name = "Rila", Countries = { bulgaria } };
            var musala = new Peak { Name = "Musala", Mountain = rila, Elevation = 2925 };

            context.Countries.Add(bulgaria);
            context.Mountains.Add(rila);
            context.Peaks.Add(musala);
            context.SaveChanges();

            var countriesQuery = context.Countries.Select(c =>
                                new
                                {
                                    ContryName = c.Name,
                                    Mountains = c.Mountains.Select(m =>
                                        new
                                        {
                                            m.Name,
                                            m.Peaks
                                        })
                                });

            foreach (var country in countriesQuery)
            {
                Console.WriteLine("Country: " + country.ContryName);
                foreach (var mountain in country.Mountains)
                {
                    Console.WriteLine("     Mountain: " + mountain.Name);
                    foreach (var peak in mountain.Peaks)
                    {
                        Console.WriteLine("\t{0} ({1})", peak.Name, peak.Elevation);
                    }
                }
            }
        }
        static void Main()
        {
            var context = new MountainsContext();
            var json = File.ReadAllText(@"../../mountains.json");
            JavaScriptSerializer ser = new JavaScriptSerializer();
            var mountains = ser.Deserialize<MountainDTO[]>(json);

            foreach (var mountainDTO in mountains)
            {
                try
                {
                    AddCountryToDb(context, mountainDTO);
                    Console.WriteLine("Mountain {0} imported", mountainDTO.MountainName);
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Error: {0}", ex.Message);
                }

            }
            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();
 }