public override async Task <IAddOrUpdateEntityResult> Work(AddOrUpdateEntityCmd Op, Unit state) { try { var entity = _context.Entry(Op.Item); switch (entity.State) { case EntityState.Detached: _context.Add(Op.Item); break; case EntityState.Modified: _context.Update(Op.Item); break; default: return(new Failed($"Entity is in an incorrect state. Expected Detached or Unchanged. Got {entity.State.ToString()}")); } await _context.SaveChangesAsync(); return(new Success(entity.Entity)); } catch (Exception ex) { return(new Failed(ex.ToString())); } }
public override async Task <IUpdateEntityResult> Work(UpdateEntityCmd Op, Unit state) { try { var entity = _context.Entry(Op.Item); _context.Update(Op.Item); await _context.SaveChangesAsync(); return(new Success(entity.Entity)); } catch (Exception exp) { Console.WriteLine(exp.ToString()); return(new Failed(exp.ToString())); } }
public override async Task <IUpdateEntityResult> Work(UpdateEntityCmd Op, Unit state) { try { var entity = _context.Entry(Op.Item); if (entity.State == Microsoft.EntityFrameworkCore.EntityState.Unchanged) { _context.Update(Op.Item); await _context.SaveChangesAsync(); return(new Success(entity.Entity)); } else { return(new Failed("Incorrect entity state! Expected Unchanged, got" + entity.State)); } } catch (Exception exp) { Console.WriteLine(exp.ToString()); return(new Failed(exp.ToString())); } }
public override async Task <IAddOrUpdateResult> Work(AddOrUpdateCmd Op, Unit state) { var(valid, validationResults) = Op.Validate(); string validationMessage = ""; validationResults.ForEach(x => validationMessage += x.ErrorMessage); if (!valid) { return(new Failed(validationMessage)); } try { var entity = _ctx.Entry(Op.Item); switch (entity.State) { case EntityState.Detached: _ctx.Add(Op.Item); break; case EntityState.Unchanged: _ctx.Update(Op.Item); break; default: return(new Failed($"Entity is in an incorrect state. Expected Detached or Unchanged. Got {entity.State.ToString()}")); } await _ctx.SaveChangesAsync(); return(new Successful(entity.Entity)); } catch (Exception ex) { return(new Failed(ex.ToString())); } }