public FrayteResult AddZoneCountryPostCode(FrayteZoneCountryPostCode zonepostcode) { FrayteResult result = new FrayteResult(); try { if (zonepostcode != null) { int countryId = dbContext.Countries.Where(p => p.CountryName == zonepostcode.CountryName).Select(p => p.CountryId).FirstOrDefault(); if (countryId > 0) { if (zonepostcode.LogisticZoneCountryPostCodeId > 0) { var postcode = dbContext.LogisticZoneCountryPostCodes.Where(p => p.LogisticZoneCountryPostCodeId == zonepostcode.LogisticZoneCountryPostCodeId).FirstOrDefault(); if (postcode != null) { if (UtilityRepository.LogisticZoneCountryId(zonepostcode.OperationZoneId, countryId, zonepostcode.LogisticCompany, zonepostcode.LogisticType, zonepostcode.RateType, zonepostcode.Zone) > 0) { postcode.LogisticZoneCountryId = UtilityRepository.LogisticZoneCountryId(zonepostcode.OperationZoneId, countryId, zonepostcode.LogisticCompany, zonepostcode.LogisticType, zonepostcode.RateType, zonepostcode.Zone); postcode.FromPostCode = zonepostcode.FromPostCode; postcode.ToPostCode = zonepostcode.ToPostCode; dbContext.Entry(postcode).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); result.Status = true; } else { if (postcode.FromPostCode == zonepostcode.FromPostCode && postcode.ToPostCode == zonepostcode.ToPostCode) { result.Status = false; } else { var servicezone = (from ls in dbContext.LogisticServices join lsz in dbContext.LogisticServiceZones on ls.LogisticServiceId equals lsz.LogisticServiceId where ls.OperationZoneId == zonepostcode.OperationZoneId && ls.LogisticCompany == zonepostcode.LogisticCompany && ls.LogisticType == zonepostcode.LogisticType && ls.RateType == (zonepostcode.RateType == null ? null : zonepostcode.RateType) && lsz.ZoneName == zonepostcode.Zone select new { lsz.LogisticServiceZoneId }).FirstOrDefault(); LogisticServiceZoneCountry lszc = new LogisticServiceZoneCountry(); lszc.OperationZoneId = zonepostcode.OperationZoneId; lszc.LogisticServiceZoneId = servicezone.LogisticServiceZoneId; lszc.CountryId = countryId; dbContext.LogisticServiceZoneCountries.Add(lszc); if (lszc != null) { dbContext.SaveChanges(); } LogisticZoneCountryPostCode lzcpc = new LogisticZoneCountryPostCode(); lzcpc.LogisticZoneCountryId = lszc.LogisticZoneCountryId; lzcpc.FromPostCode = zonepostcode.FromPostCode; lzcpc.ToPostCode = zonepostcode.ToPostCode; dbContext.LogisticZoneCountryPostCodes.Add(lzcpc); if (lzcpc != null) { dbContext.SaveChanges(); } result.Status = true; } } } } else { var servicezone = (from ls in dbContext.LogisticServices join lsz in dbContext.LogisticServiceZones on ls.LogisticServiceId equals lsz.LogisticServiceId join lszc in dbContext.LogisticServiceZoneCountries on lsz.LogisticServiceZoneId equals lszc.LogisticServiceZoneId where ls.OperationZoneId == zonepostcode.OperationZoneId && ls.LogisticCompany == zonepostcode.LogisticCompany && ls.LogisticType == zonepostcode.LogisticType && ls.RateType == (zonepostcode.RateType == null ? null : zonepostcode.RateType) && lsz.ZoneName == zonepostcode.Zone && lszc.CountryId == countryId select new { lszc.LogisticZoneCountryId }).FirstOrDefault(); if (servicezone != null) { LogisticZoneCountryPostCode lzcpc = new LogisticZoneCountryPostCode(); lzcpc.LogisticZoneCountryId = servicezone.LogisticZoneCountryId; lzcpc.FromPostCode = zonepostcode.FromPostCode; lzcpc.ToPostCode = zonepostcode.ToPostCode; dbContext.LogisticZoneCountryPostCodes.Add(lzcpc); if (lzcpc != null) { dbContext.SaveChanges(); } result.Status = true; } else { LogisticServiceZoneCountry lszc = new LogisticServiceZoneCountry(); lszc.OperationZoneId = zonepostcode.OperationZoneId; lszc.LogisticServiceZoneId = UtilityRepository.LogisticServiceZoneId(zonepostcode.OperationZoneId, zonepostcode.LogisticCompany, zonepostcode.LogisticType, zonepostcode.RateType, zonepostcode.Zone); lszc.CountryId = countryId; dbContext.LogisticServiceZoneCountries.Add(lszc); if (lszc != null) { dbContext.SaveChanges(); } LogisticZoneCountryPostCode lzcpc = new LogisticZoneCountryPostCode(); lzcpc.LogisticZoneCountryId = lszc.LogisticZoneCountryId; lzcpc.FromPostCode = zonepostcode.FromPostCode; lzcpc.ToPostCode = zonepostcode.ToPostCode; dbContext.LogisticZoneCountryPostCodes.Add(lzcpc); if (lzcpc != null) { dbContext.SaveChanges(); } result.Status = true; } } } } return(result); } catch (Exception ex) { return(result); } }