/// <summary>
        /// This will delete an item from the database
        /// </summary>
        /// <param name="keys">The keys must be given in the same order as entity framework has them</param>
        /// <returns></returns>
        public async Task <ISuccessOrErrors> DeleteAsync <TEntity>(params object[] keys) where TEntity : class
        {
            var keyProperties = _db.GetKeyProperties <TEntity>();

            if (keyProperties.Count != keys.Length)
            {
                throw new ArgumentException("The number of keys in the data entry did not match the number of keys provided");
            }

            var entityToDelete = await _db.Set <TEntity>().FindAsync(keys);

            if (entityToDelete == null)
            {
                return
                    (new SuccessOrErrors().AddSingleError(
                         "Could not delete entry as it was not in the database. Could it have been deleted by someone else?"));
            }

            _db.Set <TEntity>().Remove(entityToDelete);
            var result = await _db.SaveChangesWithCheckingAsync();

            if (result.IsValid)
            {
                result.SetSuccessMessage("Successfully deleted {0}.", typeof(TEntity).Name);
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This will delete an item from the database
        /// </summary>
        /// <param name="keys">The keys must be given in the same order as entity framework has them</param>
        /// <returns></returns>
        public async Task <ISuccessOrErrors> DeleteAsync <TEntity>(params object[] keys) where TEntity : class
        {
            var entityToDelete = await _db.Set <TEntity>().FindAsync(keys).ConfigureAwait(false);

            if (entityToDelete == null)
            {
                return
                    (new SuccessOrErrors().AddSingleError(
                         "Could not delete entry as it was not in the database. Could it have been deleted by someone else?"));
            }

            _db.Set <TEntity>().Remove(entityToDelete);
            var result = await _db.SaveChangesWithCheckingAsync().ConfigureAwait(false);

            if (result.IsValid)
            {
                result.SetSuccessMessage("Successfully deleted {0}.", typeof(TEntity).Name);
            }

            return(result);
        }