예제 #1
0
        public override int SaveChanges()
        {
            var changeInfo = ChangeTracker.Entries()
                 .Where(
                     t =>
                         t.State == EntityState.Added || t.State == EntityState.Modified
                // || t.State == EntityState.Deleted
                         ).ToList();

            var commitId = Guid.NewGuid();
            var commitedOnUTC = DateTime.UtcNow;

            foreach (var objInfo in changeInfo)
            {

                if (objInfo.Entity is ModelBase)
                {
                    var objBase = objInfo.Entity as ModelBase;

                    switch (objInfo.State)
                    {
                        case EntityState.Added:
                            objBase.CreatedBy = Thread.CurrentPrincipal.Identity.Name;
                            objBase.CreatedOn = DateTime.UtcNow;
                            break;
                        //case EntityState.Deleted:
                        case EntityState.Modified:
                            objBase.UpdatedBy = Thread.CurrentPrincipal.Identity.Name;
                            objBase.UpdatedOn = DateTime.UtcNow;
                            break;
                    }
                }

                var audit = new EntityChangeAudit
                {
                    EntityType = objInfo.Entity.GetType().FullName,
                    ChangeState = objInfo.State.ToString(),
                    CommitId = commitId,
                    CommitedOnUTC = commitedOnUTC,
                    CommitedOnLocal = DateTime.Now,
                    CommitedBy = Thread.CurrentPrincipal.Identity.Name
                };

                // PropertyName: Before|After
                var changedProperties = new Dictionary<string, EntityChangeAudit.ChangePropertyPair>();

                foreach (var propName in objInfo.CurrentValues.PropertyNames)
                {
                    changedProperties.Add(propName, new EntityChangeAudit.ChangePropertyPair
                    {
                        PropertName = propName,
                        Before = objInfo.State == EntityState.Added ? null : objInfo.OriginalValues[propName],
                        After = objInfo.State == EntityState.Deleted ? null : objInfo.CurrentValues[propName]
                    });
                }

                audit.ChangedValues = JsonConvert.SerializeObject(changedProperties, new JsonSerializerSettings
                {
                    TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
                });

                EntityChangeAudits.Add(audit);
            }

            return base.SaveChanges();
        }
예제 #2
0
        public override int SaveChanges()
        {
            var changeInfo = ChangeTracker.Entries()
                             .Where(
                t =>
                t.State == EntityState.Added || t.State == EntityState.Modified
                // || t.State == EntityState.Deleted
                ).ToList();

            var commitId      = Guid.NewGuid();
            var commitedOnUTC = DateTime.UtcNow;

            foreach (var objInfo in changeInfo)
            {
                if (objInfo.Entity is ModelBase)
                {
                    var objBase = objInfo.Entity as ModelBase;

                    switch (objInfo.State)
                    {
                    case EntityState.Added:
                        objBase.CreatedBy = Thread.CurrentPrincipal.Identity.Name;
                        objBase.CreatedOn = DateTime.UtcNow;
                        break;

                    //case EntityState.Deleted:
                    case EntityState.Modified:
                        objBase.UpdatedBy = Thread.CurrentPrincipal.Identity.Name;
                        objBase.UpdatedOn = DateTime.UtcNow;
                        break;
                    }
                }

                var audit = new EntityChangeAudit
                {
                    EntityType      = objInfo.Entity.GetType().FullName,
                    ChangeState     = objInfo.State.ToString(),
                    CommitId        = commitId,
                    CommitedOnUTC   = commitedOnUTC,
                    CommitedOnLocal = DateTime.Now,
                    CommitedBy      = Thread.CurrentPrincipal.Identity.Name
                };

                // PropertyName: Before|After
                var changedProperties = new Dictionary <string, EntityChangeAudit.ChangePropertyPair>();

                foreach (var propName in objInfo.CurrentValues.PropertyNames)
                {
                    changedProperties.Add(propName, new EntityChangeAudit.ChangePropertyPair
                    {
                        PropertName = propName,
                        Before      = objInfo.State == EntityState.Added ? null : objInfo.OriginalValues[propName],
                        After       = objInfo.State == EntityState.Deleted ? null : objInfo.CurrentValues[propName]
                    });
                }

                audit.ChangedValues = JsonConvert.SerializeObject(changedProperties, new JsonSerializerSettings
                {
                    TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
                });

                EntityChangeAudits.Add(audit);
            }

            return(base.SaveChanges());
        }