如你所知,我們可以在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 - 對於Delete
和Modify
存在一些問題,
刪除:我覺得作爲我審計dbEntry.Entity
,它正在尋找當前值和拋出異常:
CurrentValues不能用於處於已刪除狀態的實體。
修改:總是OldValue
和NewValue
始終是一樣的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;
}
已解決,但明天可以張貼爲答案! – Aria