private static void AddTypes(IEnumerable<string> types ) { var context = new traintimetable_dbEntities(); foreach (var t in types) { context.StationTypes.Add(new StationType() { Name = t }); context.SaveChanges(); } }
private static void Insert(List<EcrModel> parsed) { try { var test = parsed.Select(x => x.StationImageUrl).Distinct(); var context = new traintimetable_dbEntities(); AddTypes(test); var count = 0; foreach (var ecrModel in parsed) { var newStation = new Station() { Ecr = ecrModel.Ecr, ExpressCode = ecrModel.ExpressCode, OpenStreetMapNode = ecrModel.OpenStreetMapNode, StationName = ecrModel.StationName, StationType = context.StationTypes.FirstOrDefault(x => x.Name == ecrModel.StationImageUrl), OpenStreetMapUrl = ecrModel.OpenStreetMapUrl, }; if (ecrModel.Position != null) { newStation.Position = new Position() { Latitude = ecrModel.Position.Latitude, Longitude = ecrModel.Position.Longitude }; } else { newStation.Position = new Position() { Latitude = 0, Longitude = 0 }; } try { var wikiImages = _wikiImages.FirstOrDefault(x => x.Ecr == ecrModel.Ecr); if (wikiImages != null && wikiImages.ByteImage != null) { var urlblob = InsertToBlob(wikiImages.FileName, wikiImages.ByteImage); var smallBlob = InsertToBlob("thumb_" + wikiImages.FileName, wikiImages.Thumb); newStation.Image=new Image(); newStation.Image.FullImageUrl = urlblob; newStation.Image.ThumbUrl = smallBlob; } else { var url = string.Format( "http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/{0},{1}/17?mapSize=800,800&key=l6AOGVgaf0DIlLuOEOum~ysMXqmfV9te9mCzpSy1QHA~AsNLrgOgd3BHryIGeusH0s4--NnOwoiAT6Pc56ilxJPflOrSOF1CN1h0BaimBW1g&AB&pp={0},{1};;★", ecrModel.Position.Latitude.ToString().Replace(",", "."), ecrModel.Position.Longitude.ToString().Replace(",", ".")); var url2 = string.Format( "http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/{0},{1}/17?mapSize=300,300&key=l6AOGVgaf0DIlLuOEOum~ysMXqmfV9te9mCzpSy1QHA~AsNLrgOgd3BHryIGeusH0s4--NnOwoiAT6Pc56ilxJPflOrSOF1CN1h0BaimBW1g&AB&pp={0},{1};;★", ecrModel.Position.Latitude.ToString().Replace(",", "."), ecrModel.Position.Longitude.ToString().Replace(",", ".")); var img = new WebClient().DownloadData(url); var thumb = new WebClient().DownloadData(url2); if (img != null) { var urlblob = InsertToBlob(ecrModel.StationName+".jpg", img); var smallBlob = InsertToBlob("thumb_"+ecrModel.StationName + ".jpg", thumb); newStation.Image = new Image(); newStation.Image.FullImageUrl = urlblob; newStation.Image.ThumbUrl = smallBlob; } } } catch (Exception d) { } context.Stations.Add(newStation); context.SaveChanges(); Console.WriteLine(count++); } } catch (Exception e) { } }