/// <summary> /// Updates a model, returning the updated model from the database /// </summary> /// <param name="model"></param> /// <returns></returns> public TEntity Update(TEntity model) { TEntity retVal = null; var pkValue = _repositoryUtilities.GetPKValue(model); var exists = _entitycontext.Find(pkValue); if (null != exists && !IsSystemAccount(exists) && !IsModelSystemType(exists)) { var repoUtilities = new RepoUtilities <TEntity>(); retVal = repoUtilities.ExecuteInRetryLoop(() => { TEntity ret; ret = _entitycontext.Attach(model); _context.Entry(model).State = EntityState.Modified; _context.SaveChanges(); return(ret); }); } else { throw new Exception(@"Cannot update a model that doesn't exist in the database."); } return(retVal); }
/// <summary> /// Creates a new model in the database /// </summary> /// <param name="model"></param> /// <returns></returns> public TEntity Create(TEntity model) { TEntity retVal = null; if (!IsSystemAccount(model)) { var repoUtilities = new RepoUtilities <TEntity>(); retVal = repoUtilities.ExecuteInRetryLoop(() => { TEntity ret; _repositoryUtilities.SetPKValue(model, Guid.NewGuid()); ret = _entitycontext.Add(model); _context.Entry(model).State = EntityState.Added; _context.SaveChanges(); return(ret); }); } else { throw new Exception("model cannot be of system type"); } return(retVal); }