public async Task <AskBoard> InsertArticle(AskBoard ab) { try { this.ab = ab; await ValidateUser(); if (errorList.Count == 0) { context.AskBoard.Add(ab); await context.SaveChangesAsync(); int newId = ab.Id; ab.ParentId = newId; await UpdateArticle(ab); return(await GetArticle(ab.Id)); } else { throw new Exception(new ErrorManager().ErrorList(errorList)); } } catch (Exception ex) { throw ex; } }
public async Task <AskBoard> UpdateArticle(AskBoard ab) { try { this.ab = ab; await ValidateAsk(); if (errorList.Count == 0) { DetachedKey(ab); context.AskBoard.Update(ab); await context.SaveChangesAsync(); return(await GetArticle(ab.Id)); } else { throw new Exception(new ErrorManager().ErrorList(errorList)); } } catch (Exception ex) { throw ex; } }
void DetachedKey(AskBoard ab) { // to avoid // "The instance of entity type cannot be tracked because another instance of this type with the same key is already being tracked" var local = context.Set <AskBoard>() .Local .FirstOrDefault(entry => entry.Id.Equals(ab.Id)); if (local != null) { context.Entry(local).State = EntityState.Detached; } context.Entry(ab).State = EntityState.Modified; }
public async Task <bool> DeleteArticle(int Id) { try { this.ab = await GetArticle(Id); if (ab != null) { context.AskBoard.Remove(ab); await context.SaveChangesAsync(); return(true); } else { throw new Exception(new ErrorManager().ErrorList(errorList)); } } catch (Exception ex) { throw ex; } }