Entity Framework 6 (code first) entity versioning and auditing

前端 未结 3 616
傲寒
傲寒 2020-12-30 09:15

I\'m looking at using Entity Framework 6.1.1 with SQL Server 2008 R2.

Currently I\'m creating my models and database using the code-first EF feature. My basic use-c

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 09:54

    I have used the 2nd approach you mention, by overloading the dbContext SaveChanges() method:

    public class MyContext : DbContext
    {
    
     public int SaveChanges(int userId)
     {
        // Get all Added/Deleted/Modified entities (not Unmodified or Detached)
        foreach (var ent in this.ChangeTracker.Entries().Where(p => p.State ==  EntityState.Added 
        || p.State == EntityState.Deleted || p.State == EntityState.Modified))
        {
    
            foreach (AuditLog x in GetAuditRecordsForChange(ent, userId))
            {
                this.AuditLogs.Add(x);
            }
        }
        return base.SaveChanges();
      }
    ...
    

    So if I want to log a particular entity, I just call the overloaded SaveChanges & pass in a UserId:

    public void Update(StockCatalogueItem entity, int userId)
    {
         _context.SaveChanges(userId);
    }
    

    I also have a custom DoNotLog attribute which I use to decorate the entity properties that I don't want to log. Without this, the logging could generate a huge amount of data, as each entity modification equals one db entry.

    [DoNotLog]
    public int CreatedBy { get; set; }
    

    The GetAuditRecordsForChange method does the checking for any DoNotLog properties and returns a List which gets saved in the AuditLogs table:

    public class AuditLog
        {
            public int Id { get; set; }
            public int CreatedBy { get; set; }
            public DateTime CreatedOn { get; set; }
            public AuditEventType EventType { get; set; }
            public string TableName { get; set; }
            public int EntityId { get; set; }
            public string ColumnName { get; set; }
            public string Controller { get; set; }
            public string Action { get; set; }
            public string IPAddress { get; set; }
            public string OriginalValue { get; set; }
            public string NewValue { get; set; }
        }
    

提交回复
热议问题