locked
How to check which properties have been modified in a LINQ Object and save in a log table in database ? RRS feed

  • Question

  • User539757411 posted

    Hi, I have an ASP.NET Webforms system using LINQ to Entity and I want to save in a database all the data that the user has modified.
    Does anyone tell me if there is a better way or do I have to manually check the field, the previous value and the current value and save it if it is different, and then I save in a "LOG" file in database ? Can anyone indicate a better way to do it?

    Thanks in advance.

    Monday, October 19, 2020 1:01 PM

All replies

  • User475983607 posted

    Hi, I have an ASP.NET Webforms system using LINQ to Entity and I want to save in a database all the data that the user has modified.
    Does anyone tell me if there is a better way or do I have to manually check the field, the previous value and the current value and save it if it is different, and then I save in a "LOG" file in database ? Can anyone indicate a better way to do it?

    There's no other way.  At some point there needs to be a comparison.   You can encapsulate the logic using generics so you don't see it or have to write the same type of code.

    Monday, October 19, 2020 3:03 PM
  • User539757411 posted
    db.ChangeTracker.DetectChanges();
                    //var modificacoes = db.ChangeTracker
                    //    .Entries()
                    //    .Where(x => x.State == EntityState.Modified)
                    //    .Select(s => s.Entity)
                    //    .ToList();
    
                    var changes = from ent in db.ChangeTracker.Entries()
                                  where ent.State != EntityState.Unchanged
                                  select ent;
    
                    foreach (var change in changes)
                    {
                        if (change.State == EntityState.Added)
                        {
                            // Log Added
                        }
                        else if (change.State == EntityState.Modified)
                        {
                            // Log Modified
                            var item = change.Cast<proponente>().Entity;
                            var originalValues = db.Entry(item).OriginalValues;
                            var currentValues = db.Entry(item).CurrentValues;
    
                            foreach (string propertyName in originalValues.PropertyNames)
                            {
                                var original = originalValues[propertyName];
                                var current = currentValues[propertyName];
    
                                if (!Equals(original, current))
                                {
                                    log_alteracoesproponentes logalteracoes = new log_alteracoesproponentes();
                                    logalteracoes.campo = propertyName;
                                    logalteracoes.dataalteracao = DateTime.Now;
                                    logalteracoes.idUsuario = 1; //ALTERAR
                                    logalteracoes.valorAntigo = original.ToString();
                                    logalteracoes.valorNovo = current.ToString();
                                    db.log_alteracoesproponentes.Add(logalteracoes);
                                    db.SaveChanges();
                                }
                            }
    
                        }
                        else if (change.State == EntityState.Deleted)
                        {
                            // log deleted
                        }
                    }

    I'm trying something like this.

    Wednesday, October 21, 2020 4:40 PM