出于审计日志记录的目的,我重写了 EF 4.1 数据库优先方法中的 SaveChanges()
方法。
我有所有 ObjectStateEntry 对象,我想知道我是否可以从每个 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
}
}
最佳答案
我还没有测试过,但像这样的东西应该可以工作:
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;
}
关于c# - 从 Entity Framework 中的 ObjectStateEntry 获取所有键及其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876428/