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(); }
static void Main(string[] args) { Database.SetInitializer(new MigrateDatabaseToLatestVersion <MountainsContext, Configuration>()); var context = new MountainsContext(); var peaks = context.Peaks.Select(p => new { Name = p.Name }); Console.WriteLine(string.Join(", ", peaks)); }
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(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); }
static void Main() { var context = new MountainsContext(); var query = context.Mountains.Select(m => new { m.Name, Countries = m.Countries.Select(c => c.Name), Peaks = m.Peaks.Select(p => p.Name).ToList() }); foreach (var m in query) { Console.WriteLine("{0} ; {1} ; {2}", m.Name, String.Join(", ", m.Countries), String.Join(", ", m.Peaks)); } }
static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion <MountainsContext, MountainsDatabaseMigrationConfiguration>()); var context = new MountainsContext(); var query = context.Mountains.Select(m => new { m.Name, Countries = m.Countries.Select(c => c.Name), Peaks = m.Peaks.Select(p => p.Name) }); foreach (var m in query) { Console.WriteLine("{0} ; {1} ; {2}", m.Name, String.Join(", ", m.Countries), String.Join(", ", m.Peaks)); } }
private static void AddCountryToDb(MountainsContext context, MountainDTO mountainDTO) { if(mountainDTO.MountainName == null) { throw new Exception("Mountain name is required"); } var m = new Mountain() { Name = mountainDTO.MountainName }; foreach (var peak in mountainDTO.Peaks) { if(peak.PeakName == null) { throw new Exception("Peak name is required"); } if(peak.Elevation == null) { throw new Exception("Peak elevation is required"); } var peakToDb = new Peak() { Name = peak.PeakName, Elevation = peak.Elevation.GetValueOrDefault() }; m.Peaks.Add(peakToDb); } 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 }; } country.Mountains.Add(m); context.Countries.Add(country); } context.Mountains.Add(m); }
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(); }
static void Main(string[] args) { Database.SetInitializer(new MountainsMigrationStrategy()); //Country c = new Country() {Code = "AB" , Name = "Absurdistan"}; //Mountain m = new Mountain() {Name = "Absurditan Hills" }; //m.Peaks.Add(new Peak() {Name = "Great Peak", Mountain = m}); //m.Peaks.Add(new Peak() {Name = "Small Peak", Mountain = m}); //c.Mountains.Add(m); //var context = new MountainsContext(); //context.Countries.Add(c); //context.SaveChanges(); var context = new MountainsContext(); var countriesQuery = context.Countries.Select(c => new { CountryName = c.Name, Mountains = c.Mountains.Select(m => new { m.Name, m.Peaks }) }); foreach (var country in countriesQuery) { Console.WriteLine("Country: " + country.CountryName); 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); } } } }