Пример #1
0
        private void sb_saveStateToDB(StateCityJsonModel stateJson, bool saveCitiesForEachState)
        {
            using (var entityLogistic = new Model.logisticEntities())
            {
                bool add = false;
                Model.PWS0StateCities entryPWS0State = entityLogistic.PWS0StateCities.FirstOrDefault(o => o.StateId == stateJson.StateID &&
                                                                                                     o.isState.HasValue && o.isState.Value);

                if (entryPWS0State == null)
                {
                    add            = true;
                    entryPWS0State = new Model.PWS0StateCities();
                }
                entryPWS0State.CityId              = null;
                entryPWS0State.CityName            = null;
                entryPWS0State.isState             = true;
                entryPWS0State.ParentIdInDB        = null;
                entryPWS0State.ParentStateIdInJson = null;
                entryPWS0State.StateId             = stateJson.StateID;
                entryPWS0State.StateName           = stateJson.StateName;

                if (add)
                {
                    entityLogistic.PWS0StateCities.Add(entryPWS0State);
                }

                entityLogistic.SaveChanges();
                //to get id of stationid
                int parentIdInDB = entryPWS0State.Id;

                if (saveCitiesForEachState && stateJson.StateID.HasValue)
                {
                    this.sb_readAndSaveCitiesToDB(stateJson.StateID.Value, parentIdInDB);
                }
            }
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cityJson"></param>
        /// <param name="parentIdInDB">refer to Id which is saved automatically in db</param>
        /// <param name="parentStateIdInJson">refer to stateId which comes from JSON</param>
        private void sb_saveCityToDB(StateCityJsonModel cityJson, int parentStateIdInJson, int?parentIdInDB)
        {
            using (var entityLogistic = new Model.logisticEntities())
            {
                if (!parentIdInDB.HasValue)
                {
                    var entryState = entityLogistic.PWS0StateCities.FirstOrDefault(o => o.ParentStateIdInJson == parentStateIdInJson && (o.isState.HasValue && o.isState.Value));
                    if (entryState != null)
                    {
                        parentIdInDB = entryState.Id;
                    }
                }
                bool add = false;
                Model.PWS0StateCities entryPWS0City = entityLogistic.PWS0StateCities.FirstOrDefault(o => o.CityId == cityJson.CityID &&
                                                                                                    (!o.isState.HasValue || !o.isState.Value));

                if (entryPWS0City == null)
                {
                    add           = true;
                    entryPWS0City = new Model.PWS0StateCities();
                }
                entryPWS0City.CityId              = cityJson.CityID;
                entryPWS0City.CityName            = cityJson.CityName;
                entryPWS0City.isState             = false;
                entryPWS0City.ParentIdInDB        = parentIdInDB;
                entryPWS0City.ParentStateIdInJson = parentStateIdInJson;
                entryPWS0City.StateId             = null;
                entryPWS0City.StateName           = null;
                if (add)
                {
                    entityLogistic.PWS0StateCities.Add(entryPWS0City);
                }

                entityLogistic.SaveChanges();
            }
        }