public override int SaveChanges()
        {
            int retVal;

            try
            {
                auditList.Clear();
                list.Clear();
                auditFactory = new AuditTrailFactory(this);
                var entityList = ChangeTracker.Entries().Where(p => p.State == System.Data.Entity.EntityState.Added || p.State == System.Data.Entity.EntityState.Deleted || p.State == System.Data.Entity.EntityState.Modified);
                foreach (var entity in entityList)
                {
                    tblAudit audit = auditFactory.GetAudit(entity);
                    if (audit != null)
                    {
                        audit.UserId = this.UserId;
                        auditList.Add(audit);
                        list.Add(entity);
                    }
                }

                retVal = base.SaveChanges();
                if (auditList.Count > 0)
                {
                    foreach (var audit in auditList)
                    {//add all audits
                        this.tblAudits.Add(audit);
                    }
                    base.SaveChanges();
                }
            }
            catch (DbEntityValidationException e)
            {
                this.logger.Error("SaveChanges", e);
                string strEr = "";
                foreach (var eve in e.EntityValidationErrors)
                {
                    strEr = "Entity of type \"{0}\" in state \"{1}\" has the following validation errors:";
                    strEr = string.Format(strEr, eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    this.logger.Error(strEr);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        strEr = "- Property: \"{0}\", Error: \"{1}\"";
                        strEr = string.Format(strEr, ve.PropertyName, ve.ErrorMessage);
                        this.logger.Error(strEr);
                    }
                }

                throw e;
            }
            catch (Exception ex)
            {
                this.logger.Error("SaveChanges", ex);
                throw ex;
            }
            return(retVal);
        }
Beispiel #2
0
        public tblAudit GetAudit(DbEntityEntry entry)
        {
            tblAudit audit = new tblAudit();

            try
            {
                if (!IsAuditable(entry.Entity))
                {
                    return(null);
                }

                audit.AuditId       = Guid.NewGuid();
                audit.TableName     = GetTableName(entry);
                audit.RevisionStamp = DateTime.Now;
                audit.TableIdValue  = GetKeyValue(entry);

                //entry is Added
                if (entry.State == System.Data.Entity.EntityState.Added)
                {
                    audit.NewData = GetEntryValueInString(entry, false);
                    audit.Actions = Constants.AuditActions.Insert.ToString();
                }
                //entry in deleted
                else if (entry.State == System.Data.Entity.EntityState.Deleted)
                {
                    audit.OldData = GetEntryValueInString(entry, true);
                    audit.Actions = Constants.AuditActions.Deleted.ToString();
                }
                //entry is modified
                else if (entry.State == System.Data.Entity.EntityState.Modified)
                {
                    audit.OldData = GetEntryValueInString(entry, true);
                    audit.NewData = GetEntryValueInString(entry, false);
                    audit.Actions = Constants.AuditActions.Updated.ToString();
                }
            }
            catch (Exception ex)
            {
                this.logger.Error("GetAudit", ex);
                throw ex;
            }
            return(audit);
        }