0

Be gentle, Its my first post so sorry if I don't get the terminology right first time.

In Entity Framework I need to write some auditing code.

I have a context class which inherits DbContext which has my Database. SetInitializer and My DbSets for each entity.

I also have an override of SaveChanges where I am doing the auditing.

There are 3 levels of auditing, None, Basic and AllProperties and each Entity has a different property assigned to it, in this case I am trying to do an AllProperties audit.

This works for EntityState.Modified where I am using GetModifiedProperties in a foreach to write a new record for each property amended

But I am having trouble doing the same for EntityState.Added, there doesn't seem to be an equivalent GetNewProperties that I can loop through.

case AuditType.AllProperties:
{
  if (entry.State == EntityState.Modified)
  {
    foreach (var propertyName in entry.GetModifiedProperties()
      .Where(propertyName => propertyName != "Id" && propertyName != "RowVersion"))
      {
        var original = entry.OriginalValues;
        var oldValue = original.GetValue(original.GetOrdinal(propertyName)).ToString();

        var current = entry.CurrentValues;
        var newValue = current.GetValue(current.GetOrdinal(propertyName)).ToString();

        //if (oldValue != newValue) // probably not necessary
        //{

        AuditTrails.Add(new AuditTrail
          {
            DomainId = domainId,
            Controller = "",
            Action = "",
            EntityType = entry.Entity.GetType().Name,
            EntityId = entityId,
            Property = propertyName,
            Before = oldValue,
            After = newValue
           });
       }
     }
     else
     {

       AuditTrails.Add(new AuditTrail
         {
           DomainId = domainId,
           Controller = "",
           Action = "",
           EntityType = entry.Entity.GetType().Name,
           EntityId = entityId,
           EntityValue = entry.Entity.ToString(),
           Property = "",
           Before = "",
           After = entry.State.ToString()
         });
}

Can anyone help?

Frazer
  • 560
  • 2
  • 11
  • 21
  • Just guessing but new record they would all be new properties. So is there a GetAllProperties or GetProperties? [maybe this will help](http://stackoverflow.com/questions/5851274/how-to-get-all-names-of-properties-in-an-entity) – musefan Oct 22 '13 at 11:24

2 Answers2

0

Whenever you call entry.OriginalValues, you have access to the PropertyNames property, which will give you a list of all the properties. Since it's a new entry, they are all new anyway.

ESG
  • 8,988
  • 3
  • 35
  • 52
0

The below now works for me at the moment

if (entry.State == EntityState.Added)
                    {
                        for (var i = 0; i < entry.CurrentValues.FieldCount; i++)
                        {
                            var propertyName = entry.CurrentValues.DataRecordInfo.FieldMetadata[i].FieldType.Name;

                            if (propertyName == "Id") continue;
                            if (propertyName == "RowVersion") continue;

                            AuditTrails.Add(new AuditTrail
                                                {
                                                    DomainId = domainId,
                                                    Controller = "",
                                                    Action = entry.State.ToString(),
                                                    EntityType = entityType,
                                                    EntityId = entityId,
                                                    EntityValue = "", //entityValue,
                                                    Property = propertyName,
                                                    Before = "",
                                                    After = entry.CurrentValues[i].ToString(),
                                                });
                        }
                    }
Frazer
  • 560
  • 2
  • 11
  • 21