Exemple #1
0
        /// <summary>
        /// Deletes the first specified entity, if it exists. Returns entity or null (if it doesn't exist).
        /// If specification doesn't match only one entity, then the first one found will be deleted.
        /// </summary>
        /// <typeparam name="T">The entity type to delete.</typeparam>
        /// <param name="spec">An entity specification.</param>
        /// <returns>The deleted entity or null (if no such entity).</returns>
        public T Delete <T>(ISpecification <T> spec, bool hardDelete = false, params Expression <Func <T, object> >[] includes) where T : BaseEntity
        {
            IQueryable <T> set = DataContext.ISet <T>();

            foreach (var include in includes)
            {
                set = set.Include(include);
            }
            var entity = set.FirstOrDefault(spec.AsExpression());

            if (entity != null)
            {
                if (!hardDelete)
                {
                    entity.IsDeleted = true;
                }
                else
                {
                    DataContext.ISet <T>().Remove(entity);
                }
            }
            return(entity);
        }
Exemple #2
0
 public void AddEntity <T>(T o) where T : BaseEntity
 {
     DataContext.ISet <T>().Add(o);
 }