2

I have the following Update generic method for my entities:

public void Update < T > (T entity) where T: class {
    DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
    if (dbEntityEntry.State == System.Data.Entity.EntityState.Detached) {

        DbContext.Set < T > ().Attach(entity);

    }
    dbEntityEntry.State = System.Data.Entity.EntityState.Modified;
}

After SaveChanges() the data is successfully updated in the DB.

Now I nee to implement and Audit Log before SaveChanges() but I noticed that CurrentValues are equal to OriginalValues:

// For updates, we only want to capture the columns that actually changed
if (!object.Equals(dbEntry.OriginalValues.GetValue<object>(propertyName), dbEntry.CurrentValues.GetValue<object>(propertyName))){

  //here I add a new Audit Log entity

}

Any clue on how to solve this? Or is there a better way to do it in Entity Framework 6?

VAAA
  • 14,531
  • 28
  • 130
  • 253

2 Answers2

2

If you are using a disconnected entity, you can set originals values without affect entity instance values, adapt this method at you needs

public static void LoadOriginalValues(this WorkflowsContext db, DbEntityEntry entity) 
        {
            var props = entity.GetDatabaseValues();

            foreach (var p in props.PropertyNames)
            {
                if (entity.Property(p).IsModified)
                {
                    entity.Property(p).OriginalValue = props[p];
                }
            }
        }  
Orestes G.
  • 31
  • 2
  • Thanks, this worked for me. I was using Auditing and checking the values. With attach the original values were equal to the current when using attach. Now using the entitty.getdatabasevalues its working fine. – Mark Dec 23 '21 at 09:39
1

The original values are recovered from the entity itself. If the entity is being tracked by a context, this information is available.

In your case, you're using a disconected entity, so there is no change tracking, and the entity doesn't have the original values.

SO, in this case, if you need the original values there is no other option than getting them from the DB, and compare them, one by one.

If you want to get an entity that behaves as if it had been tracked by the context you can use a context to read the entity from the DB, and use something like ValueInjecter to automatically set the property values from the disconected entity into the tracked entity.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • You are totally right!! I need to get from the DB the original entity and then do the compare. Don't know if that could slow down the perfomance of the app. – VAAA May 31 '14 at 00:49
  • 1
    Of course, it's slower: you hit the database twice, and that is relatively expensive. However, don't worry about application performance if you don't have that problem.In other words: don't spend any time in improving performance if you don't have a performace problem. You'd rathe remploy that time in implementing features. Please, if you think this is the right answer, accept it. – JotaBe Jun 01 '14 at 01:15