/// <summary> /// Enregistre ou met a jour un parking /// </summary> public void Save() { if (this.Id == Guid.Empty) { this.Metier.Id = Guid.NewGuid(); ServiceParking.Insert(this.Metier); } else { ServiceParking.Update(this.Metier); } }
public static void AddtoBase() { List <Parking> parkingToAdd = new List <Parking>(); //recupere les parkings using (ParkingContext context = new ParkingContext()) { using (WebClient wc = new WebClient()) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://data.citedia.com/r1/parks/timetable-and-prices"); StreamReader reader; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { reader = new StreamReader(response.GetResponseStream()); List <string[]> csvfile = new List <string[]>();// = reader.ReadToEnd(); string line; while ((line = reader.ReadLine()) != null) { csvfile.Add(Regex.Matches(line, @"[\""].+?[\""]|[^;]+").Cast <Match>().Select(m => m.Value).ToArray()); } reader.Close(); int count = 0; foreach (string[] stringtab in csvfile) { if (count != 0) { var json = wc.DownloadString("http://data.citedia.com/r1/parks/" + stringtab[0]); dynamic data = JsonConvert.DeserializeObject <ParkInformation>(json); Adresse adresse = new Adresse(Guid.NewGuid(), stringtab[3], 0, 35000, "rennes", "france", true); Parking parking = new Parking(Guid.NewGuid(), RemoveDiacritics(stringtab[0]), adresse, data.max, data.free, RemoveDiacritics(stringtab[2]), RemoveDiacritics(stringtab[1]), data.status); ServiceAdresse.Insert(adresse, context); ServiceParking.Insert(parking, context); //Suppression des anciennes données ServiceParking.DeleteAll(); ServiceAdresse.DeleteParking(); parkingToAdd.Add(parking); } count++; } } context.Parkings.AddRange(parkingToAdd); context.SaveChanges(); } } }