Getting same Current and Original value of change tracker modified state

310 Views Asked by At

Below is my override saveChanges Methed which calls SetChanges Method

 public override int SaveChanges(bool acceptAllChangesOnSuccess)
        {
            SetChanges();
            OnBeforeSaving();
            return base.SaveChanges(acceptAllChangesOnSuccess);
        }

Right now, Sometimes code works completely fine but in some scenario It gives same value of both property.OriginalValue and property.CurrentValue for Modification so I am not able find what is the issue in my code

 private void SetChanges()
        {
            Guid SystemLogId = Guid.NewGuid();
            var currentDate = DateTime.Now;
            var entitiesTracker = ChangeTracker.Entries()
                .Where(p => p.State == EntityState.Modified || p.State == EntityState.Added).ToList();

            foreach (var entry in entitiesTracker)
            {
                var pagename = entry.Entity.GetType().Name;
                if (pagename != "ExceptionLog")
                {
                    var rowid = 0;
                    try
                    {
                        rowid = int.Parse(entry.OriginalValues["Id"].ToString());
                    }
                    catch (Exception)
                    { }

                    SystemLog sysLog = new SystemLog();
                   

                    List<SystemChangeLog> changeLog = new List<SystemChangeLog>();
                    foreach (PropertyEntry property in entry.Properties)
                    {
                        string propertyName = property.Metadata.Name;
                        switch (entry.State)
                        {
                            case EntityState.Added:
                                sysLog.Event = "Created";
                                break;

                            case EntityState.Modified:
                                {
                                    sysLog.Event = "Updated";
                                    if (propertyName != "ModifiedDate" && propertyName != "CreatedDate" && propertyName != "ModifiedBy" && propertyName != "CreatedBy" && propertyName != "RowVersion")
                                    {
                                        var original = Convert.ToString(property.OriginalValue);
                                        var current = Convert.ToString(property.CurrentValue);

                                        if (property.IsModified && !original.Equals(current))
                                        {
                                            SystemChangeLog log = new SystemChangeLog()
                                            {
                                                Property = propertyName,
                                                OldValue = original,
                                                NewValue = current,
                                                DateOfChange = currentDate,
                                                rowid = rowid,
                                                SystemLogId = SystemLogId.ToString(),
                                            };
                                            changeLog.Add(log);
                                        }
                                    }
                                }
                                break;
                        }          
                    }
                    base.Set<SystemChangeLog>().AddRange(changeLog);
                    if(changeLog.Count() >0 || entry.State == EntityState.Added)
                    {
                        sysLog.UserId = UserId;
                        sysLog.Date = currentDate;
                        sysLog.Page = pagename;
                        sysLog.Location = ExceptionHandler(entry, "Location");
                        sysLog.IPAddress = ExceptionHandler(entry, "IPAddress");
                        sysLog.MACAddress = ExceptionHandler(entry, "MACAddress");
                        sysLog.SystemLogId = SystemLogId.ToString();
                        base.Set<SystemLog>().Add(sysLog);
                    }  
                }
            }
        }

And also Is there any way to make it fast for more than thousand entry

1

There are 1 best solutions below

0
miemengniao On

hope below code can help:

 public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
     setChanges(); // to get new value and old value
     var result = base.SaveChanges(acceptAllChangesOnSuccess);
     OnAfterSaveChanges();// to get auto added id
     return result;

}