我花了一些時間閱讀了一些關於審計追蹤的文章和文章,但仍然無法弄清楚。首先用EF 4.2代碼攔截並記錄更改
我需要的是一個非常基本的審計系統,這將使我的結果,如:
- 客戶「張三」是由
- 客戶「李四」被創造「用戶」「刪除其他用戶李四」
- 從地址‘用戶’被修改‘’
- 客戶‘其它用戶‘
我’被刪除’李四試圖覆蓋上的DbContext調用SaveChanges事件,但我被困具有以下問題:
public override int SaveChanges()
{
foreach (DbEntityEntry<IAuditable> entry in ChangeTracker.Entries<IAuditable>())
{
if (entry.State == EntityState.Added)
{
// since the object was not added yet, if I write to log in here and
// for some reason SaveChanges fail, I will end up with a fake log entry
}
else if (entry.State == EntityState.Modified)
{
// same in here
}
}
return base.SaveChanges();
// here the state for all entries have changed to Unchanged or Detached.
// detached is probably the one that was deleted however the 「Unchanged」
// could be new or modified records.
}
我知道我可以使用數據庫觸發器上做到這一點,但我想保持它在這裏讓我有更多的控制在它之上,因爲我不是一個SQL人,部署應用程序後,我不會有太多的控制數據庫。
我很確定我在這裏錯過了很簡單的東西。 我感謝任何幫助。
在此先感謝。
性能怎麼樣?在將DbContext轉換爲ObjectContext後保存數據不是較慢? – Saber
@Saber:DbContext內部使用ObjectContext,所以不應該有任何性能影響。 –