public override async Task <IAddOrUpdateResult> Work(AddOrUpdateCmd Op, Unit state) { try { var entity = _ctx.Entry(Op.Item); switch (entity.State) { case EntityState.Detached: _ctx.Add(Op.Item); break; case EntityState.Unchanged: _ctx.Attach(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())); } }
public override async Task <IAddEntityResult> Work(AddEntityCmd Op, Unit state) { try { var entity = _context.Entry(Op.Item); if (entity.State == Microsoft.EntityFrameworkCore.EntityState.Detached) { _context.Add(Op.Item); await _context.SaveChangesAsync(); return(new Success(entity.Entity)); } else { return(new Failed("Incorrect entity state! Expected Detached, 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())); } }