문제

For audit logging purpose I override SaveChanges() method in EF 4.1 Database-First approach .

I have all ObjectStateEntry object and I'm wondering if I could get all keys and their values from each ObjectStateEntry .

   IEnumerable<ObjectStateEntry> changes = this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified);
    foreach (ObjectStateEntry stateEntryEntity in changes)
    {
        if (!stateEntryEntity.IsRelationship &&
                stateEntryEntity.Entity != null &&
                    !(stateEntryEntity.Entity is DBAudit))
        {
          list<object , object> KeyValues = GetAllKeyValues(stateEntryEntity );
          //Do log all keyvalues
        }
    }
도움이 되었습니까?

해결책

I haven't tested it but something like this should work:

private Dictionary<string, object> GetAllKeyValues(ObjectStateEntry entry)
{
    var keyValues = new Dictionary<string, object>();
    var currentValues = entry.CurrentValues;
    for (int i = 0; i < currentValues.FieldCount; i++)
    {
        keyValues.Add(currentValues.GetName(i), currentValues.GetValue(i));
    }
    return keyValues;
}

다른 팁

Try using ObjectStateEntry.EntityKey and EntityKey.EntityKeyValues:

var keyValues = stateEntityEntry.EntityKey.EntityKeyValues;

which returns an array of EntityKeyMember. You can then use the Key and Value properties, which return a string and object respectively.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top