您应该启用审核员
var auditConfiguration = AuditConfiguration.Default;
auditConfiguration.IncludeRelationships = true;
auditConfiguration.LoadRelationships = true;
auditConfiguration.DefaultAuditable = true;
只需将其添加到您的应用程序初始化的位置,例如 Global.asax.cs
已编辑
据我所知,您无法使用 EF.Extended 获取旧值。这是我的解决方案:
覆盖 Context 中的 SaveChanges 方法
public override int SaveChanges()
{
return SaveChanges(false);
}
public int SaveChanges(bool disableAudit)
{
var result = -1;
try
{
if (!disableAudit)
{
foreach (var entity in ChangeTracker.Entries().Where(x => x.State == EntityState.Added ||
x.State == EntityState.Modified ||
x.State == EntityState.Deleted))
{
ProccessAuditLog(entity);
}
}
}
catch (Exception ex)
{
// handle the ex here
}
finally
{
//save changes
result = base.SaveChanges();
}
return result;
}
并添加处理审计日志的方法:
private void ProccessAuditLog(DbEntityEntry entry)
{
var entity = entry.Entity;
var entityType = GetEntityType(entity.GetType());
var oldValue = Activator.CreateInstance(entityType); ;
if (entry.State == EntityState.Modified)
{
// entry.OriginalValues doesn't load navigation properties for changed entity so we should reload the object from db to get it
// save current values
var newValue = Activator.CreateInstance(entityType);
Mapper.DynamicMap(entity, newValue, entity.GetType(), entityType);
// reload old values for entity from the db
entry.Reload();
Mapper.DynamicMap(entry.Entity, oldValue, entity.GetType(), entityType);
// revert reloading changes in entity
entry.CurrentValues.SetValues(newValue);
entry.OriginalValues.SetValues(oldValue);
entry.State = EntityState.Modified;
entity = newValue;
}
if (entry.State == EntityState.Deleted)
{
// reload old values for entity from the db
entry.Reload();
Mapper.DynamicMap(entry.Entity, oldValue, entity.GetType(), entityType);
// revert reloading changes in entity
entry.OriginalValues.SetValues(oldValue);
entry.State = EntityState.Deleted;
entity = null;
}
// here is you can proccess old entity in 'oldValue' and new entity in 'entity'
// then save your log to db using SaveChanges(true) to prevent StackOverFlow exception
}
你Mapper
可以使用AutoMapper
获取基本实体类型而不是代理类型的方法:
private Type GetEntityType(Type entityType)
{
return entityType.BaseType != null && entityType.Namespace == "System.Data.Entity.DynamicProxies"
? entityType.BaseType
: entityType;
}
希望对你有帮助