コード例 #1
0
        private void sb_saveStateToDB(StateCityJsonModel stateJson, bool saveCitiesForEachState)
        {
            using (var entityLogestic = new logisticEntities())
            {
                bool            add            = false;
                PWS0StateCities entryPWS0State = entityLogestic.PWS0StateCities.FirstOrDefault(o => o.StateId == stateJson.StateID &&
                                                                                               o.isState.HasValue && o.isState.Value);

                if (entryPWS0State == null)
                {
                    add            = true;
                    entryPWS0State = new 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)
                {
                    entityLogestic.PWS0StateCities.Add(entryPWS0State);
                }

                entityLogestic.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 entityLogestic = new logisticEntities())
            {
                if (!parentIdInDB.HasValue)
                {
                    var entryState = entityLogestic.PWS0StateCities.FirstOrDefault(o => o.ParentStateIdInJson == parentStateIdInJson && (o.isState.HasValue && o.isState.Value));
                    if (entryState != null)
                    {
                        parentIdInDB = entryState.Id;
                    }
                }
                bool            add           = false;
                PWS0StateCities entryPWS0City = entityLogestic.PWS0StateCities.FirstOrDefault(o => o.CityId == cityJson.CityID &&
                                                                                              (!o.isState.HasValue || !o.isState.Value));

                if (entryPWS0City == null)
                {
                    add           = true;
                    entryPWS0City = new 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)
                {
                    entityLogestic.PWS0StateCities.Add(entryPWS0City);
                }

                entityLogestic.SaveChanges();
            }
        }