通过实体框架后,我有几个关于在实体框架中实施审计的问题。实体框架中的审计
我想将每个创建或更新的列值存储到不同的审计表中。
现在我调用SaveChanges(false)来保存数据库中的记录(仍然没有重置上下文中的更改)。然后获取添加|修改记录并通过GetObjectStateEntries循环。但不知道如何获取存储过程填充其值的列的值。即创建,修改等。
下面是我正在处理的示例代码。
// Get the changed entires(ie, records) IEnumerable<ObjectStateEntry> changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified); // Iterate each ObjectStateEntry(for each record in the update/modified collection) foreach (ObjectStateEntry entry in changes) { // Iterate the columns in each record and get thier old and new value respectively foreach (var columnName in entry.GetModifiedProperties()) { string oldValue = entry.OriginalValues[columnName].ToString(); string newValue = entry.CurrentValues[columnName].ToString(); // Do Some Auditing by sending entityname, columnname, oldvalue, newvalue } } changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added); foreach (ObjectStateEntry entry in changes) { if (entry.IsRelationship) continue; var columnNames = (from p in entry.EntitySet.ElementType.Members select p.Name).ToList(); foreach (var columnName in columnNames) { string newValue = entry.CurrentValues[columnName].ToString(); // Do Some Auditing by sending entityname, columnname, value } }
这不是很清楚你想做的事,你你正在寻找这里 - 究竟做什么不知道该怎么办? – 2009-09-03 18:38:46
我想做审计(当对给定实体进行任何更改时,在审计表中插入表名/实体名,列名,值) – 2009-09-03 19:19:29