2017-04-18 50 views
1

如你所知,我們可以在SaveChange()審覈對象,但我有一些問題,修改和刪除實體如下,保持在SaveChange作爲DbEntityEntry.Entity新舊價值爲審覈

首先我使用Audit.Net(這是使用方法很簡單)來審覈我的對象下面的方法應該做的審計爲:

private int SaveAuditRecord(DbEntityEntry dbEntry, string userGUID) 
    { 
     Logging.Log(LoggingMode.Error, "Saving Audid Entry{0}....", dbEntry.ToString()); 
     DateTime changeTime = DateTime.UtcNow; 
     TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault() as TableAttribute; 
     string tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name; 
     Logging.Log(LoggingMode.Error, "TableName Retrived {0}...", tableName); 
     switch (dbEntry.State) 
     { 
      case System.Data.Entity.EntityState.Added: 
       int result = base.SaveChanges(); 
       AuditNet.AuditMonoAction("Add " + tableName, userGUID, dbEntry.Entity); 
       return result; 
      case System.Data.Entity.EntityState.Deleted: 
       AuditNet.AuditMonoAction("Delete " + tableName, userGUID, dbEntry.Entity); 
       break; 
      case System.Data.Entity.EntityState.Modified: 
       int modifiedResult = 0; 
       var primaryKey = GetPrimaryKeyValue(dbEntry); 
       modifiedResult = base.SaveChanges(); 
       object entity2Audit = dbEntry.Entity;//Hold the current entity here and change it in below Audit using block 
       using (var audit = Audit.Core.AuditScope.Create("Edit " + tableName,() => dbEntry.Entity)) 
       { 
        audit.SetCustomField("UserGUID", userGUID, false); 
        foreach (var prop in dbEntry.OriginalValues.PropertyNames) 
        { 
         var originalValue = dbEntry.OriginalValues[prop].ToString(); 
         var currentValue = dbEntry.CurrentValues[prop].ToString(); 
         if (originalValue != currentValue) 
          Logging.Log(LoggingMode.Error, "Prop {0} get originalValue :{1} , currentValue:{2}", prop, originalValue, currentValue); 
        } 
       } 
       return modifiedResult; 
      default: 
       break; 
     } 
     return base.SaveChanges(); 
    } 

1 - 爲了增加狀態,其中有因爲我審計dbEntry.Entity我認爲包含當前值總是沒有問題的。

2 - 對於DeleteModify存在一些問題,

刪除:我覺得作爲我審計dbEntry.Entity,它正在尋找當前值和拋出異常:

CurrentValues不能用於處於已刪除狀態的實體。

修改:總是OldValueNewValue始終是一樣的CurrentValue正如我上面提到。

所以我的主要問題是如何做到刪除和修改像dbEntry.Entity

P.S: 在使用塊,我們可以更改審計對象,將NewValue設置爲Audit.Net提到。

UPDATE1:

即使使用這樣的代碼舊的和新的價值始終是相同的:

int modRes = 0; 
       object entity2Audit = dbEntry.Entity;//Hold the current entity here and change it in below Audit using block 
       DbEntityEntry dbEntryTemp = dbEntry; 
       foreach (var prop in dbEntryTemp.OriginalValues.PropertyNames) 
       { 
        var originalValue = dbEntry.OriginalValues[prop].ToString(); 
        var currentValue = dbEntry.CurrentValues[prop].ToString(); 
        if (originalValue != currentValue) 
        { 
         Logging.Log(LoggingMode.Error, "Prop11 {0} get originalValue :{1} , currentValue:{2}", prop, originalValue, currentValue); 
         dbEntryTemp.CurrentValues[prop] = dbEntryTemp.OriginalValues[prop] = originalValue; 
        } 
       } 
       object en = dbEntryTemp.Entity; 
       using (var audit = Audit.Core.AuditScope.Create("Edit " + tableName,() => en)) 
       { 
        modRes = base.SaveChanges(); 
        audit.SetCustomField("UserGUID", userGUID, false); 
        audit.SetCustomField("NewValue", dbEntry.Entity, false); 
        audit.SetCustomField("OldValue", en, false); 
        en = dbEntry.Entity; 
       } 
+1

已解決,但明天可以張貼爲答案! – Aria

回答

1

首先,我要感謝thepirat000誰做Audit.Net lib下,這是非常好的LIB審計與許多選項,因爲他提到GitHub

但關於我的問題,這是我第一次覆蓋SaveChange(),我不知道DbEntityEntry ob ject,但解決方案似乎很有趣,只需要將CurrentValuesOriginalValues轉換爲Object(),在修改情況下,我得到的舊值爲dbEntry.OriginalValues.ToObject(),並在Audit.Net範圍內將其更改爲dbEntry.CurrentValues.ToObject(),即全部。

Logging.Log(LoggingMode.Prompt, "Saving Audid Entry{0}....", dbEntry.ToString()); 
     TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault() as TableAttribute; 
     string tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name; 
     Logging.Log(LoggingMode.Prompt, "TableName Retrived {0}...", tableName); 
     switch (dbEntry.State) 
     { 
      case System.Data.Entity.EntityState.Added: 
       int result = base.SaveChanges(); 
       object addedObj = dbEntry.Entity; 
       using (var audit = Audit.Core.AuditScope.Create("Add " + tableName,() =>addedObj)) 
       { 
        audit.SetCustomField("UserGUID", userGUID); 
        addedObj = null; 
       } 
       return result; 
      case System.Data.Entity.EntityState.Deleted: 
       object deletedObj = dbEntry.Entity; 
       using (var audit = Audit.Core.AuditScope.Create("Delete " + tableName,() => deletedObj)) 
       { 
        audit.SetCustomField("UserGUID", userGUID); 
        deletedObj = null; 
       } 
       break; 
      case System.Data.Entity.EntityState.Modified: 
       object en = dbEntry.OriginalValues.ToObject(); 
       using (var audit = Audit.Core.AuditScope.Create("Edit " + tableName,() => en)) 
       { 
        audit.SetCustomField("UserGUID", userGUID); 
        en = dbEntry.CurrentValues.ToObject(); 
       } 
       break; 
      default: 
       break; 
     } 
     return base.SaveChanges();