尝试实施
中的以下审核代码对于密钥名称,它一直给出错误“序列不包含匹配元素”
我不想编辑系统生成的文件,因为对我的模型的任何更新都会导致我必须返回并进行编辑。我已使用im附加了部分和元数据类,但不知何故,上面找到的审计功能不是读取键值。
解决这个问题的运气好吗?
我首先使用ef5 DB并使用部分和元数据类
的元数据:
public class TABLEMetadata : ABCEntities.IDescribableEntity,
ABCEntities.IAuditable
{
[Key]
public int X_ID { get; set; }
[Required(ErrorMessage = "A Name is required!")]
public string X_Name { get; set; }
public int Deleted { get; set; }
public string Describe()
{
return "{ X_ID : \"" + X_ID + "\", X_Name : \"" + X_Name + "\" }";
}
}
PARTIAL CLASS
[MetadataType(typeof(TABLE.Metadata))]
public partial class TABLE
{
private sealed class Metadata
{
[Key]
public int X_ID { get; set; }
}
}
public partial class ABCEntities
{
public interface IAuditable
{
int Deleted { get; set; }
}
public interface IDescribableEntity
{
string Describe();
}
public int SaveChangesNew()
{
ChangeTracker.DetectChanges(); // Important!
var userId = System.Web.HttpContext.Current.User.Identity.Name;
foreach (var ent in this.ChangeTracker.Entries().Where(p => p.State == System.Data.EntityState.Added || p.State == System.Data.EntityState.Deleted || p.State == System.Data.EntityState.Modified))
{
foreach (X_AuditLog x in GetAuditRecordsForChange(ent, userId))
{
this.X_AuditLog.Add(x);
}
}
// Call the original SaveChanges(), which will save both the changes made and the audit records
return base.SaveChanges();
}
private List<X_AuditLog> GetAuditRecordsForChange(DbEntityEntry dbEntry, string userId)
{
var auditEntity = dbEntry.Entity as IAuditable;
List<X_AuditLog> result = new List<X_AuditLog>();
DateTime changeTime = DateTime.Now;
TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute;
string tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name;
var keyObject = dbEntry.Entity.GetType().GetProperties().SingleOrDefault(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0);
string keyName = keyObject != null ? keyObject.Name : "";
if (dbEntry.State == System.Data.EntityState.Added)
{
auditEntity.Deleted = 0;
result.Add(new X_AuditLog()
{
AuditLogID = Guid.NewGuid(),
UserID = userId,
EventDateUTC = changeTime,
EventType = "A", // Added
TableName = tableName,
RecordID = dbEntry.CurrentValues.GetValue<object>(keyName).ToString(),
ColumnName = "*ALL",
NewValue = (dbEntry.CurrentValues.ToObject() is IDescribableEntity) ? (dbEntry.CurrentValues.ToObject() as IDescribableEntity).Describe() : dbEntry.CurrentValues.ToObject().ToString()
});
}
else if (dbEntry.State == System.Data.EntityState.Modified)
{
foreach (string propertyName in dbEntry.OriginalValues.PropertyNames)
{
if (!object.Equals(dbEntry.GetDatabaseValues().GetValue<object>(propertyName),
dbEntry.CurrentValues.GetValue<object>(propertyName)))
{
result.Add(new X_AuditLog()
{
AuditLogID = Guid.NewGuid(),
UserID = userId,
EventDateUTC = changeTime,
EventType = "M", // Modified
TableName = tableName,
RecordID = dbEntry.OriginalValues.GetValue<object>(keyName).ToString(),
ColumnName = propertyName,
OriginalValue = dbEntry.OriginalValues.PropertyNames.ToString(),
NewValue = dbEntry.CurrentValues.GetValue<object>(propertyName) == null ? null : dbEntry.CurrentValues.GetValue<object>(propertyName).ToString()
});
}
}
}
else if (dbEntry.State == System.Data.EntityState.Deleted)
{
dbEntry.State = EntityState.Unchanged;
auditEntity.Deleted = 1; //perform soft delete
result.Add(new X_AuditLog()
{
AuditLogID = Guid.NewGuid(),
UserID = userId,
EventDateUTC = changeTime,
EventType = "D", // Deleted
TableName = tableName,
RecordID = dbEntry.OriginalValues.GetValue<object>(keyName).ToString(),
ColumnName = "*ALL",
NewValue = (dbEntry.OriginalValues.ToObject() is IDescribableEntity) ? (dbEntry.OriginalValues.ToObject() as IDescribableEntity).Describe() : dbEntry.OriginalValues.ToObject().ToString()
});
}
return result;
}
}