public void InsertCity(dynamic address) { CM_S_CITY_BOOK Item = new CM_S_CITY_BOOK(); EntityMapper.UpdateEntity(address, Item); Item.DT_INSERT = Item.DT_UPDATE = DateTime.UtcNow; Item.USERINSERT = Item.USERUPDATE = Settings.Value.Username; Item.CITY_NAME = Item.CITY_NAME.ToUpperInvariant(); Item.ROWGUID = Guid.NewGuid(); if (string.IsNullOrWhiteSpace(Item.COUNTRY_CODE)) { Item.COUNTRY_CODE = Settings.Value.CountryCode; } CM_S_AREA_BOOK Area = null; if (!string.IsNullOrWhiteSpace(Item.AREA_CODE)) { Area = DBContext.CM_S_AREA_BOOK.FirstOrDefault(E => E.AREA_CODE == Item.AREA_CODE); } if (Area == null) //Revert to default state area, if possibile { string StateCode = address.StateCode; CM_S_STATE_EXT_AUS State = DBContext.CM_S_STATE_EXT_AUS.FirstOrDefault(E => E.STATE_CODE == StateCode); if (State == null) { throw new InvalidOperationException($"Cannot find a state with state code = '{StateCode}'"); } Item.AREA_CODE = State.DEFAULT_AREA_CODE; } Area = DBContext.CM_S_AREA_BOOK.FirstOrDefault(E => E.AREA_CODE == Item.AREA_CODE); if (Area == null) { throw new InvalidOperationException($"Cannot find area with code = '{Item.AREA_CODE}' for state '{address.StateCode}'"); } CM_S_CITY_BOOK City = DBContext.CM_S_CITY_BOOK.Where(E => E.COUNTRY_CODE == Item.COUNTRY_CODE && E.AREA_CODE == Item.AREA_CODE && E.ZIP_CODE == Item.ZIP_CODE).OrderByDescending(E => E.CITY_COUNTER).FirstOrDefault(); Item.CITY_COUNTER = (short)(City != null ? City.CITY_COUNTER + 1 : 1); DBContext.CM_S_CITY_BOOK.Add(Item); DBContext.SaveChanges(); }
public bool CheckAddressCityData(string StateCode, string City, ref string AreaCode, ref string ZipCode, out short?cityCounter, out string errorMessage) { if (string.IsNullOrWhiteSpace(StateCode)) { throw new ArgumentNullException(nameof(StateCode)); } if (string.IsNullOrWhiteSpace(ZipCode)) { throw new ArgumentNullException(nameof(ZipCode)); } cityCounter = null; errorMessage = null; if (string.IsNullOrWhiteSpace(AreaCode)) { AreaCode = DBContext.CM_S_STATE_EXT_AUS.FirstOrDefault(E => E.STATE_CODE == StateCode)?.DEFAULT_AREA_CODE; if (string.IsNullOrWhiteSpace(AreaCode)) { throw new InvalidOperationException($"Cannot find State for code '{StateCode}'"); } } ; if (City != null) { string Area = AreaCode; string Zip = ZipCode; /* * var predicate = PredicateBuilder.New<CM_S_CITY_BOOK>(E => E.CITY_NAME == City.ToUpperInvariant()); * if (!string.IsNullOrWhiteSpace(AreaCode)) * predicate = predicate.And(E => E.AREA_CODE == Area); * if (!string.IsNullOrWhiteSpace(ZipCode)) * predicate = predicate.And(E => E.ZIP_CODE == Zip); */ var predicate = PredicateBuilder.New <CM_S_CITY_BOOK>(true); if (!string.IsNullOrWhiteSpace(AreaCode)) { predicate = predicate.And(E => E.AREA_CODE == Area); } if (!string.IsNullOrWhiteSpace(ZipCode)) { predicate = predicate.And(E => E.ZIP_CODE == Zip); } CM_S_CITY_BOOK city = DBContext.CM_S_CITY_BOOK.FirstOrDefault(predicate); if (city == null) { errorMessage = $"City '{City}'"; if (!string.IsNullOrWhiteSpace(AreaCode)) { errorMessage += $" and Area Code '{AreaCode}'"; } if (!string.IsNullOrWhiteSpace(ZipCode)) { errorMessage += $"and Zip code '{ZipCode}'"; } errorMessage += $" are not correct, please check data."; return(false); } AreaCode = city?.AREA_CODE; ZipCode = city?.ZIP_CODE; cityCounter = city?.CITY_COUNTER ?? 0; } return(true); }