如何获取实体/表的名称和用于添加审核的记录编号



我正在尝试使用 EF DbContext 为我的系统执行审核方法。我覆盖了"保存更改"事件。但是,我有两个困难需要帮助。我在互联网上和StackOverflow上读了很多书,但我仍然无法找到解决方案。你可以帮我吗?

1) 添加记录时无法获取表的实际名称。当我将记录添加到表中时,只需出现名称"对象"。其他具有该名称的操作通常。

2) 进入注册表后,我希望将注册表 ID 放入审核日志中。有什么建议吗?

如果问题如此明显,我期待道歉,但我真的不明白如何进行。

public partial class coletasEntities : DbContext
{
    public override int SaveChanges()
    {
        ChangeTracker.DetectChanges();
        foreach (var change in ChangeTracker.Entries())
        {
            if (change is auditoria || change.State == EntityState.Detached || change.State == EntityState.Unchanged)
                continue;
            //Pega o nome da tabela
            var entityName = change.Entity.GetType().Name;
            //var entityName2 = change.Entity.GetType().BaseType()
            if (entityName == "auditoria") continue;
            // Get the Table() attribute, if one exists
            //TableAttribute tableAttr = change.Entity.GetType().BaseType.Name;
            TableAttribute tableAttr = change.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute;
            // Get table name (if it has a Table attribute, use that, otherwise get the pluralized name)
            //string tableName = tableAttr != null ? tableAttr.Name : ((TableAttribute)tableAttr[0]).Name;
            //string tableName = tableAttr != null ? tableAttr.Name : change.Entity.GetType().Name;
            string tableName = tableAttr != null ? tableAttr.Name : change.Entity.GetType().BaseType.Name;
            // Get primary key value (If you have more than one key column, this will need to be adjusted)
            //string keyName = change.Entity.GetType().GetProperties().Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).Name;
            if (change.State == EntityState.Added)
            {
                using (coletasEntities ctx = new coletasEntities())
                {
                    foreach (var prop in change.CurrentValues.PropertyNames)
                    {
                        auditoria audit = new auditoria();
                        audit.campo = prop.ToString();
                        audit.data = DateTime.Now.Date;
                        audit.hora = DateTime.Now.ToShortTimeString();
                        audit.id_registro = 0;
                        audit.id_usuario = Global.id_usuario;
                        audit.tabela = tableName;
                        audit.tipo_operacao = (int)change.State;
                        audit.valor_antigo = "";
                        try
                        {
                            audit.valor_novo = change.CurrentValues[prop].ToString();
                        } catch
                        {
                            audit.valor_novo = "";
                        }
                        ctx.auditoria.Add(audit);
                        ctx.SaveChanges();
                    }
                }
                continue;
            }
            else if (change.State == EntityState.Deleted)
            {
                using (coletasEntities ctx = new coletasEntities())
                {
                    foreach (var prop in change.OriginalValues.PropertyNames)
                    {
                        auditoria audit = new auditoria();
                        audit.campo = prop.ToString();
                        audit.data = DateTime.Now.Date;
                        audit.hora = DateTime.Now.ToShortTimeString();
                        audit.id_registro = 0;
                        audit.id_usuario = Global.id_usuario;
                        audit.tabela = tableName;
                        audit.tipo_operacao = (int)change.State;
                        try
                        {
                            audit.valor_antigo = change.OriginalValues[prop].ToString();
                        }
                        catch
                        {
                            audit.valor_antigo = "";
                        }

                        audit.valor_novo = "";

                        ctx.auditoria.Add(audit);
                        ctx.SaveChanges();
                    }
                }
                continue;
            }
            else if (change.State == EntityState.Modified)
            {
                using (coletasEntities ctx = new coletasEntities())
                {
                    foreach (var prop in change.OriginalValues.PropertyNames)
                    {
                        auditoria audit = new auditoria();
                        audit.campo = prop.ToString();
                        audit.data = DateTime.Now.Date;
                        audit.hora = DateTime.Now.ToShortTimeString();
                        audit.id_registro = 0;
                        audit.id_usuario = Global.id_usuario;
                        audit.tabela = tableName;
                        audit.tipo_operacao = (int)change.State;
                        try
                        {
                            audit.valor_antigo = change.OriginalValues[prop].ToString();
                        }
                        catch
                        {
                            audit.valor_antigo = "";
                        }
                        try
                        {
                            audit.valor_novo = change.CurrentValues[prop].ToString();
                        }
                        catch
                        {
                            audit.valor_novo = "";
                        }
                        if (audit.valor_antigo != audit.valor_novo)
                        {
                            ctx.auditoria.Add(audit);
                            ctx.SaveChanges();
                        }
                    }
                }
                continue;
            }
            else
            {
                // Otherwise, don't do anything, we don't care about Unchanged or Detached entities
            }
        }
        return base.SaveChanges();
    }
}

有什么建议吗?

使用此 auditEntry.TableName = entry。Metadata.Relational().表名;下面是一个示例:

       foreach (var entry in ChangeTracker.Entries())
        {
            if (entry.Entity is Audit || entry.State == EntityState.Detached || 
            entry.State == EntityState.Unchanged)
                continue;
            var auditEntry = new AuditEntry(entry);
            auditEntry.TableName = entry.Metadata.Relational().TableName;
            auditEntries.Add(auditEntry);
            foreach (var property in entry.Properties)
            {
                if (property.IsTemporary)
                {
                   
                    auditEntry.TemporaryProperties.Add(property);
                    continue;
                }
                string propertyName = property.Metadata.Name;
                if (property.Metadata.IsPrimaryKey())
                {
                    auditEntry.KeyValues[propertyName] = property.CurrentValue;
                    continue;
                }
                switch (entry.State)
                {
                    case EntityState.Added:
                        auditEntry.NewValues[propertyName] = property.CurrentValue;
                        break;
                    case EntityState.Deleted:
                        auditEntry.OldValues[propertyName] = property.OriginalValue;
                        break;
                    case EntityState.Modified:
                        if (property.IsModified)
                        {
                            auditEntry.OldValues[propertyName] = property.OriginalValue;
                            auditEntry.NewValues[propertyName] = property.CurrentValue;
                        }
                        break;
                }
            }
        }









  public class AuditEntry
    {
        public AuditEntry()
        {
            Changes = new List<AuditDelta>();
        }
        public AuditEntry(EntityEntry entry)
        {
            Entry = entry;
            Changes = new List<AuditDelta>();
        }
        public string CreateUser { get; set; }
        public string ActionMessage { get; set; }
        public string ActionType { get; set; }
        public EntityEntry Entry { get; }
        public string TableName { get; set; }
        public int ModuleType { get; set; }
        public Dictionary<string, object> KeyValues { get; } = new Dictionary<string, object>();
        public Dictionary<string, object> ForeignKeyValues { get; } = new Dictionary<string, object>();
        public Dictionary<string, object> OldValues { get; } = new Dictionary<string, object>();
        public Dictionary<string, object> NewValues { get; } = new Dictionary<string, object>();
        public Dictionary<string, object> EnitityNameLabels { get; } = new Dictionary<string, object>();
        public List<PropertyEntry> TemporaryProperties { get; } = new List<PropertyEntry>();
        public bool HasTemporaryProperties => TemporaryProperties.Any();

        public List<AuditDelta> Changes { get; set; }
        public string DateTimeStamp  { get; set; }
        public AuditTrail ToAudit()
        {
            var audit = new AuditTrail();
            audit.tablename = TableName;
            audit.actiontype = ActionType;
            audit.CreateUser = CreateUser;
            audit.moduletype = ModuleType;
            audit.actionmessage = ActionMessage;
            audit.CreateDate = DateTime.UtcNow;
            audit.primarykeyvalues = JsonConvert.SerializeObject(KeyValues);
            audit.foreignkeyvalues = ForeignKeyValues.Count == 0 ? null : JsonConvert.SerializeObject(ForeignKeyValues);
            audit.oldvalues = OldValues.Count == 0 ? null : JsonConvert.SerializeObject(OldValues);
            audit.newvalues = NewValues.Count == 0 ? null : JsonConvert.SerializeObject(NewValues);
            audit.enititynamelabels = EnitityNameLabels.Count == 0 ? null : JsonConvert.SerializeObject(EnitityNameLabels);
            audit.changes = Changes.Count == 0 ? null : JsonConvert.SerializeObject(Changes);
            return audit;
        }

    }
 public class AuditDelta
    {
        public string FieldName { get; set; }
        public string ValueBefore { get; set; }
        public string ValueAfter { get; set; }
        public int FieldId { get; set; }
    }

相关内容

  • 没有找到相关文章

最新更新