Esempio n. 1
0
    public override int SaveChanges()
    {
        var modifiedEntities = ChangeTracker.Entries().Where(p => p.State == EntityState.Modified).ToList();

        foreach (var change in modifiedEntities)
        {
            var entityName     = System.Data.Entity.Core.Objects.ObjectContext.GetObjectType(change.Entity.GetType()).ToString();
            var primaryKey     = GetPrimaryKeyValue(change);
            var databaseValues = change.GetDatabaseValues();

            foreach (var prop in change.OriginalValues.PropertyNames)
            {
                var originalValue = databaseValues?.GetValue <object>(prop)?.ToString();
                var currentValue  = change?.CurrentValues[prop]?.ToString();

                if (originalValue != currentValue)
                {
                    ChangeLog log = new ChangeLog()
                    {
                        EntityName      = entityName,
                        PrimaryKeyValue = primaryKey.ToString(),
                        PropertyName    = prop,
                        OldValue        = originalValue,
                        NewValue        = currentValue,
                        DateChanged     = DateTime.UtcNow,
                        ChangedBy       = HttpContext.Current.User.Identity.Name
                    };
                    ChangeLogs.Add(log);
                }
            }
        }

        return(base.SaveChanges());
    }
Esempio n. 2
0
        public override int SaveChanges()
        {
            //Do custom code
            // throw new Exception("override DbContext>SaveChanges working");
            // this.SaveChanges();


            var modifiedEntities = ChangeTracker.Entries()
                                   .Where(p => p.State == EntityState.Modified).ToList();
            var now = DateTime.UtcNow;

            foreach (var change in modifiedEntities)
            {
                var entityName = change.Entity.GetType().Name;
                var primaryKey = GetPrimaryKeyValue(change);

                foreach (var prop in change.OriginalValues.PropertyNames)
                {
                    var originalValue = change.OriginalValues[prop].ToString();
                    var currentValue  = change.CurrentValues[prop].ToString();
                    if (originalValue != currentValue)                     //Only create a log if the value changes
                    {
                        ChangeLog log = new ChangeLog()
                        {
                            EntityName      = entityName,
                            PrimaryKeyValue = primaryKey.ToString(),
                            PropertyName    = prop,
                            OldValue        = originalValue,
                            NewValue        = currentValue,
                            DateChanged     = now
                        };
                        ChangeLogs.Add(log);
                    }
                }
            }
            return(base.SaveChanges());
        }
Esempio n. 3
0
 public void AddChangeLog(IssueChangeLog changeLog)
 {
     ChangeLogs.Add(new JIssueChangeLog(changeLog));
 }
Esempio n. 4
0
        private void AddTimestamps()
        {
            var modifiedEntities = ChangeTracker.Entries()
                                   .Where(x => x.Entity is EntityBase && (x.State == EntityState.Added || x.State == EntityState.Modified)).ToList();

            foreach (var change in modifiedEntities)
            {
                var    now  = DateTime.Now; // current datetime
                string user = GetCurrentUserID();

                if (change.State == EntityState.Added)
                {
                    ((EntityBase)change.Entity).CreatedAt = now;
                    ((EntityBase)change.Entity).CreatedBy = user;
                    ((EntityBase)change.Entity).UpdatedAt = now;
                    ((EntityBase)change.Entity).UpdatedBy = user;
                }
                else
                {
                    var entityName     = change.Entity.GetType().Name;
                    var primaryKey     = GetPrimaryKeyValue(change);
                    var DatabaseValues = change.GetDatabaseValues();

                    foreach (var prop in change.OriginalValues.PropertyNames.Where(x => !x.Equals("UpdatedBy") && !x.Equals("UpdatedAt") && !x.Equals("CreatedBy") && !x.Equals("CreatedAt")))
                    {
                        //if (prop == "ItemID")
                        //{
                        //    //var x = DatabaseValues.GetValue<object>(prop).GetType();
                        //}

                        string originalValue = string.Empty;
                        string currentValue  = string.Empty;

                        if (DatabaseValues.GetValue <object>(prop) != null)
                        {
                            switch (Type.GetTypeCode(DatabaseValues.GetValue <object>(prop).GetType()))
                            {
                            case TypeCode.DateTime:
                                originalValue = string.Format("{0:yyyy-MM-dd}", DatabaseValues.GetValue <object>(prop));
                                break;

                            case TypeCode.Decimal:
                                originalValue = string.Format("{0:0.00}", DatabaseValues.GetValue <object>(prop));
                                break;

                            default:
                                originalValue = DatabaseValues.GetValue <object>(prop).ToString();
                                break;
                            }
                        }
                        if (change.CurrentValues[prop] != null)
                        {
                            switch (Type.GetTypeCode(change.CurrentValues[prop].GetType()))
                            {
                            case TypeCode.DateTime:
                                currentValue = string.Format("{0:yyyy-MM-dd}", change.CurrentValues[prop]);
                                break;

                            case TypeCode.Decimal:
                                currentValue = string.Format("{0:0.00}", change.CurrentValues[prop]);
                                break;

                            default:
                                currentValue = change.CurrentValues[prop].ToString();
                                break;
                            }
                        }
                        if (originalValue != currentValue) //Only create a log if the value changes
                        {
                            ChangeLogs.Add(new ChangeLog()
                            {
                                EntityName = entityName,
                                //NewValue = currentValue,
                                OldValue        = originalValue,
                                PrimaryKeyValue = int.Parse(primaryKey.ToString()),
                                UpdatedAt       = now,
                                UpdatedBy       = user,
                                PropertyName    = prop,
                            });
                        }
                    }
                    ((EntityBase)change.Entity).UpdatedAt = now;
                    ((EntityBase)change.Entity).UpdatedBy = user;
                }
            }
        }