Last active
July 6, 2020 11:53
-
-
Save PRElias/ccde907385c64d8bc9602da7006d876c to your computer and use it in GitHub Desktop.
C# auditoria
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override int SaveChanges(bool acceptAllChangesOnSuccess) | |
{ | |
OnBeforeSaving(); | |
return base.SaveChanges(acceptAllChangesOnSuccess); | |
} | |
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken)) | |
{ | |
OnBeforeSaving(); | |
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); | |
} | |
private void OnBeforeSaving() | |
{ | |
var entries = ChangeTracker.Entries(); | |
foreach (var entry in entries) | |
{ | |
if (entry.Entity is IAuditoria auditoria) | |
{ | |
var now = DateTime.Now; | |
switch (entry.State) | |
{ | |
case EntityState.Deleted: | |
entry.State = EntityState.Unchanged; | |
auditoria.DataAlteracao = DateTime.Now; | |
auditoria.LoginAlteracao = auditoria.LoginExecutor; | |
auditoria.IsDeleted = true; | |
base.Add(new LogUpdate{ Login = auditoria.LoginExecutor, | |
DataHora = DateTime.Now, Tabela = entry.Entity.ToString(), | |
PK = Convert.ToInt32(entry.Property("Id").CurrentValue), | |
objetoAnterior = JsonConvert.SerializeObject(entry.GetDatabaseValues().ToObject()), | |
objetoNovo = JsonConvert.SerializeObject(entry.CurrentValues.ToObject()) }); | |
break; | |
case EntityState.Modified: | |
auditoria.DataAlteracao = now; | |
auditoria.LoginAlteracao = auditoria.LoginExecutor; | |
base.Add(new LogUpdate{ Login = auditoria.LoginExecutor, | |
DataHora = DateTime.Now, Tabela = entry.Entity.ToString(), | |
PK = Convert.ToInt32(entry.Property("Id").CurrentValue), | |
objetoAnterior = JsonConvert.SerializeObject(entry.GetDatabaseValues().ToObject()), | |
objetoNovo = JsonConvert.SerializeObject(entry.CurrentValues.ToObject()) }); | |
break; | |
case EntityState.Added: | |
auditoria.DataCriacao = now; | |
auditoria.LoginCriacao = auditoria.LoginExecutor; | |
break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel.DataAnnotations; | |
using System.ComponentModel.DataAnnotations.Schema; | |
namespace Models | |
{ | |
public interface IAuditoria | |
{ | |
DateTime? DataCriacao {get; set;} | |
string LoginCriacao {get; set;} | |
DateTime? DataAlteracao {get; set;} | |
string LoginAlteracao {get; set;} | |
bool IsDeleted {get; set;} | |
[NotMapped] | |
string LoginExecutor {get; set;} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel.DataAnnotations; | |
namespace Models | |
{ | |
public class LogUpdate | |
{ | |
[Key] | |
public int Id {get; set;} | |
public string Login {get; set;} | |
public DateTime DataHora {get; set;} | |
public string Tabela {get; set;} | |
public int PK {get; set;} | |
public string objetoAnterior {get; set;} | |
public string objetoNovo {get; set;} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment