/// <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);
        }
Example #2
0
        //---------------------------------------------------------------
        //protected methods

        /// <summary>
        /// This gets the key values from this DTO in the correct order. Used in FindItemTrackedForUpdate sync/async
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected object[] GetKeyValues(IGenericServicesDbContext context)
        {
            var efkeyProperties = context.GetKeyProperties <TEntity>().ToArray();
            var dtoProperties   = typeof(TDto).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var keysInOrder     = efkeyProperties.Select(x => dtoProperties.SingleOrDefault(y => y.Name == x.Name && y.PropertyType == x.PropertyType)).ToArray();

            if (keysInOrder.Any(x => x == null))
            {
                throw new MissingPrimaryKeyException("The dto must contain all the key(s) properties from the data class.");
            }

            return(keysInOrder.Select(x => x.GetValue(this)).ToArray());
        }
        //---------------------------------------------------------------
        //protected methods

        /// <summary>
        /// This gets the key values from this DTO. Used in FindItemTrackedForUpdate sync/async
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected object[] GetKeyValues(IGenericServicesDbContext context)
        {
            var efkeyPropertyNames = context.GetKeyProperties <TEntity>().ToArray();

            var dtoKeyProperies = typeof(TDto).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                  .Where(x => efkeyPropertyNames.Any(y => y.Name == x.Name && y.PropertyType == x.PropertyType)).ToArray();

            if (efkeyPropertyNames.Length != dtoKeyProperies.Length)
            {
                throw new MissingPrimaryKeyException("The dto must contain the key(s) properties from the data class.");
            }

            return(dtoKeyProperies.Select(x => x.GetValue(this)).ToArray());
        }
Example #4
0
        //----------------------------------------------------------------------
        //non-overridable internal methods

        /// <summary>
        /// This copies back the keys from a newly created entity into the dto as long as there are matching properties in the Dto
        /// </summary>
        /// <param name="context"></param>
        /// <param name="newEntity"></param>
        internal protected void AfterCreateCopyBackKeysToDtoIfPresent(IGenericServicesDbContext context, TEntity newEntity)
        {
            var dtoKeyProperies = typeof(TDto).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var entityKeys in context.GetKeyProperties <TEntity>())
            {
                var dtoMatchingProperty =
                    dtoKeyProperies.SingleOrDefault(
                        x => x.Name == entityKeys.Name && x.PropertyType == entityKeys.PropertyType);
                if (dtoMatchingProperty == null)
                {
                    continue;
                }

                dtoMatchingProperty.SetValue(this, entityKeys.GetValue(newEntity));
            }
        }