예제 #1
0
        public void Save()
        {
            try
            {
                var auditableChanges = DatabaseChangeTracker.Entries <IBaseEntity>();
                foreach (var entry in auditableChanges)
                {
                    if (entry.Entity.Id > 0)
                    {
                        entry.State = EntityState.Modified;
                    }
                    switch (entry.State)
                    {
                    case EntityState.Added:
                        entry.Entity.IsActive = true;
                        break;

                    case EntityState.Deleted:
                        entry.Entity.IsActive = false;
                        break;

                    case EntityState.Modified:
                    {
                        break;
                    }
                    }

                    //Always make sure the last udpated by and on are set no matter what was changed
                    //entry.Entity.LastUpdatedByUserId = _currentUser.UserId;
                    entry.Entity.LastUpdatedOn = TimeFactory.Now();

                    if (ChangeListener != null)
                    {
                        ChangeListener.EntityChanged(entry);
                    }
                }

                if (AllowDebugInfo)
                {
                    Debug.WriteLine("BaseDbContext Hash Code Saving: {0}", GetHashCode());
                    try
                    {
                        var changes =
                            ChangeTracker.Entries().Where(
                                i =>
                                i.State == EntityState.Added || i.State == EntityState.Modified ||
                                i.State == EntityState.Deleted);
                        foreach (var entry in changes)
                        {
                            var finalValueData = new StringBuilder();
                            if (entry.CurrentValues != null && entry.CurrentValues.PropertyNames != null)
                            {
                                var isFirst = true;
                                foreach (var currPropertyName in entry.CurrentValues.PropertyNames)
                                {
                                    finalValueData.Append(Environment.NewLine);
                                    if (isFirst)
                                    {
                                        isFirst = false;
                                    }
                                    else
                                    {
                                        finalValueData.Append(", ");
                                    }

                                    finalValueData.Append(" [");
                                    finalValueData.Append(currPropertyName);
                                    finalValueData.Append("] = '");

                                    var currValueAsObject = entry.CurrentValues[currPropertyName];
                                    var currValue         = currValueAsObject == null ? "[null]" : currValueAsObject.ToString();
                                    var prevValue         = entry.State == EntityState.Modified && entry.OriginalValues != null
                                                        ? entry.OriginalValues[currPropertyName].ToString()
                                                        : "[null]";

                                    finalValueData.Append(currValue);
                                    finalValueData.Append("'");

                                    if (entry.State == EntityState.Modified &&
                                        !currValue.Equals(prevValue, StringComparison.CurrentCulture))
                                    {
                                        finalValueData.Append(" (was '");
                                        finalValueData.Append(prevValue);
                                        finalValueData.Append("')");
                                    }
                                }
                            }

                            var message = string.Format("{0}*{1}* {2}: {3}",
                                                        Environment.NewLine,
                                                        entry.State,
                                                        entry.Entity == null ? "[unknown]" : entry.Entity.GetType().Name,
                                                        finalValueData);

                            Trace.WriteLine(message);
                            Debug.WriteLine(message);
                        }
                    }
                    catch (Exception ex)
                    {
                        var errorMsg = string.Concat("!!! Unexpected Exception constructing Debug Info !!! - ", ex.Message);
                        Trace.WriteLine(errorMsg);
                        Debug.WriteLine(errorMsg);
                    }
                }

                base.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var validationExceptions = (from validationErrors in dbEx.EntityValidationErrors
                                            from validationError in validationErrors.ValidationErrors
                                            select
                                            new ValidationException(string.Format("Property = {0}; Error = {1}",
                                                                                  validationError.PropertyName,
                                                                                  validationError.ErrorMessage))).
                                           Cast <Exception>().ToList();
                throw new AggregateException(dbEx.Message, validationExceptions);
            }
        }