public void AddTrail(Trail t) { try { _dbContext.Trails.Add(t); var result = _dbContext.SaveChanges(); if (result > 0) { //return new RepositoryActionResult<Trail>(t, RepositoryActionStatus.Created); } else { //return new RepositoryActionResult<Trail>(t, RepositoryActionStatus.NothingModified, null); } } catch (Exception ex) { //return new RepositoryActionResult<Trail>(null, RepositoryActionStatus.Error, ex); } }
public void UpdateTrail(Trail t) { try { // you can only update when trail already exists for this id var existingTrail = _dbContext.Trails.FirstOrDefault(trail => trail.Id == t.Id); if (existingTrail == null) { //return new RepositoryActionResult<User>(g, RepositoryActionStatus.NotFound); } // change the original entity status to detached; otherwise, we get an error on attach // as the entity is already in the dbSet // set original entity state to detached _dbContext.Entry(existingTrail).State = EntityState.Detached; // attach & save _dbContext.Trails.Attach(t); // set the updated entity state to modified, so it gets updated. _dbContext.Entry(t).State = EntityState.Modified; var result = _dbContext.SaveChanges(); if (result > 0) { //return new RepositoryActionResult<Trail>(t, RepositoryActionStatus.Updated); } else { //return new RepositoryActionResult<Trail>(t, RepositoryActionStatus.NothingModified, null); } } catch (Exception ex) { //return new RepositoryActionResult<User>(null, RepositoryActionStatus.Error, ex); } }