Exemple #1
0
        public virtual void Delete(TEntity entityToDelete)
        {
            if (entityToDelete == null)
            {
                throw new ArgumentNullException("entityToUpdate");
            }

            if (_appDbContext.Entry(entityToDelete).State == EntityState.Detached)
            {
                _dbSet.Attach(entityToDelete);
            }

            _dbSet.Remove(entityToDelete);
            _appDbContext.SaveChanges();
        }
Exemple #2
0
        /// <summary>
        /// Could be used instead of SaveChanges in particular repos,
        /// But it's lightweight and usually shouldn't be chossen as a preffered way (last necessity!!!)
        /// </summary>
        public void Save()
        {
            try
            {
                BeginTransaction();
                appDbContext.SaveChanges();
                CommitTransaction();
            }
            catch (DbUpdateConcurrencyException e)
            {
                RollbackTransaction();

                StringBuilder outputLines = new StringBuilder();
                if (e.Entries != null)
                {
                    foreach (var se in e.Entries)
                    {
                        BaseModel model = se.Entity as BaseModel;

                        outputLines.AppendLine($"DbUpdateConcurrencyException: Item \"{se.Entity.GetType().Name}\" Id= \"{model.Id} \",  ModifiedOnUtc= \"{model.Modified}\"");
                    }
                }

                // TODO: Later
                // LogMessageToFile("ConcurrencyException", outputLines.ToString());

                /* Log Inner Exception is not required because we know what error we have
                 * var innerException = e.InnerException;
                 * while (innerException != null)
                 * {
                 *  LogException(innerException);
                 *  innerException = innerException.InnerException;
                 * }*/

                throw;
            }
            catch (Exception ex)
            {
                RollbackTransaction();

                // LogException(ex);

                var lastInnerException = ex.InnerException;
                var innerException     = ex.InnerException;
                while (innerException != null)
                {
                    // LogException(innerException);
                    lastInnerException = innerException;
                    innerException     = innerException.InnerException;
                }

                if (lastInnerException is SqlException)
                {
                    throw lastInnerException;
                }

                throw;
            }
            finally
            {
                foreach (EntityEntry dbEntityEntry in appDbContext.ChangeTracker.Entries())
                {
                    if (dbEntityEntry.Entity != null)
                    {
                        dbEntityEntry.State = EntityState.Detached;
                    }
                }
            }
        }