public async Task CreateAsync(SafetySetting safetySetting) { try { _dbSet.Add(safetySetting); await _context.SaveChangesAsync(); } catch (Exception ex) { LogError("There is error while updating SafetySetting: " + ex.Message, ex); } }
public void Create(SafetySetting safetySetting) { try { _dbSet.Add(safetySetting); _context.SaveChanges(); } catch (Exception ex) { LogError("There is error while updating SafetySetting: " + ex.Message, ex); } }
public async Task DeleteAsync(SafetySetting safetySetting) { try { _cacheManager.Remove(String.Format(KeyForCacheYayYo, safetySetting.Id)); _context.Database.ExecuteSqlCommand("DELETE SafetySetting WHERE Id = @p0", safetySetting.Id); await _context.SaveChangesAsync(); } catch (Exception ex) { LogError("There is error while updating data: " + ex.Message, ex); } }
public async Task UpdateAsync(SafetySetting safetySetting) { try { _cacheManager.Remove(String.Format(KeyForCacheYayYo, safetySetting.Id)); //ref:http://patrickdesjardins.com/blog/entity-framework-ef-modifying-an-instance-that-is-already-in-the-context //A way to do it is to navigate inside the local of the DbSet to see if this one is there. If the entity is present, than you detach var local = _context.Set <SafetySetting>() .Local .FirstOrDefault(f => f.Id == safetySetting.Id); if (local != null) { _context.Entry(local).State = EntityState.Detached; } _context.Entry(safetySetting).State = EntityState.Modified; await _context.SaveChangesAsync(); } catch (Exception ex) { LogError("There is error while updating SafetySetting: " + ex.Message, ex); } }
public void Update(SafetySetting safetySetting) { try { _cacheManager.Remove(String.Format(KeyForCacheYayYo, safetySetting.Id)); //ref:http://patrickdesjardins.com/blog/entity-framework-ef-modifying-an-instance-that-is-already-in-the-context //A way to do it is to navigate inside the local of the DbSet to see if this one is there. If the entity is present, than you detach var local = _context.Set <SafetySetting>() .Local .FirstOrDefault(f => f.Id == safetySetting.Id); if (local != null) { _context.Entry(local).State = EntityState.Detached; } _context.Entry(safetySetting).State = EntityState.Modified; _context.SaveChanges(); } catch (Exception ex) { //Log the error (uncomment dex variable name and add a line here to write a log. //Trace.TraceError("There is error while updating data: " + dex.InnerException); LogError("There is error while updating SafetySetting: " + ex.Message, ex); } }
public async Task <IHttpActionResult> AddSafetySettings(CreateSafetySettingModel model) { //validate model if (!ModelState.IsValid) { foreach (var key in ModelState.Keys.Where(key => ModelState[key].Errors.Count > 0)) { return(BadRequest(ModelState[key].Errors[0].ErrorMessage)); } } if (model.CancellationPin == model.DuressPin) { return(BadRequest("Cancellation Pin and Duress Pin cannot be same")); } //find safety settings var safetySetting = _safetySettingService.GetByYayYoId(model.YayYoId); try { if (safetySetting == null) { //if not found, please add new safe settings var newSafetySetting = new SafetySetting { YayYoId = model.YayYoId, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber, CancellationPin = model.CancellationPin, DuressPin = model.DuressPin }; await _safetySettingService.CreateAsync(newSafetySetting); return(Ok()); } //if Update, require old cancellationPin and old duress pin if (string.IsNullOrEmpty(model.OldCancellationPin)) { return(BadRequest("Old Cancellation Pin is required")); } if (string.IsNullOrEmpty(model.OldDuressPin)) { return(BadRequest("Old Duress Pin is required")); } //if old pin != existing pin, return error if (model.OldCancellationPin != safetySetting.CancellationPin) { return(BadRequest("Current Cancellation Pin is not correct")); } if (model.OldDuressPin != safetySetting.DuressPin) { return(BadRequest("Current Duress Pin is not correct")); } safetySetting.CancellationPin = model.CancellationPin; safetySetting.DuressPin = model.DuressPin; await _safetySettingService.UpdateAsync(safetySetting); return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }