public static IEnumerable<City> GetCities() { WeatherReportEntities dbContext = new WeatherReportEntities(); var query = from city in dbContext.Cities select new City { cityId = city.cityId, cityName = city.cityName }; return query; }
public static IEnumerable<Weather> GetWeather(int cityID) { var last24hours = DateTime.Now.AddDays(-1); WeatherReportEntities dbContext = new WeatherReportEntities(); var query = from weather in dbContext.Weathers where weather.cityId == cityID && weather.issued >= last24hours orderby weather.issued descending select new Weather { clothingLayers = weather.clothingLayers, past = weather.past, rainfall = weather.rainfall, relativeHumidity = weather.relativeHumidity, status = weather.status, temperature = weather.temperature, time = weather.time, windChill = weather.windChill, windDirection = weather.windDirection, windGustSpeed = weather.windGustSpeed, windProofLayers = weather.windProofLayers, windSpeed = weather.windSpeed, windSpeedDesc = weather.windSpeedDesc }; return query; }
public static void AddWeatherRecord(int cityId, Weather weather) { try { WeatherReportModel.Weather w = new WeatherReportModel.Weather(); w.cityId = cityId; w.clothingLayers = weather.clothingLayers; w.issued = weather.issued; w.location = weather.location; w.past = weather.past; w.pressure = weather.pressure; w.rainfall = weather.rainfall; w.relativeHumidity = weather.relativeHumidity; w.status = weather.status; w.temperature = weather.temperature; w.time = weather.time; w.windChill = weather.windChill; w.windDirection = weather.windDirection; w.windGustSpeed = weather.windGustSpeed; w.windProofLayers = weather.windProofLayers; w.windSpeed = weather.windSpeed; w.windSpeedDesc = weather.windSpeedDesc; WeatherReportEntities dbContext = new WeatherReportEntities(); dbContext.Weathers.Add(w); dbContext.SaveChanges(); } catch (DbEntityValidationException e) { //Debug.WriteLine(ex.Message); foreach (var eve in e.EntityValidationErrors) { Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }