【发布时间】:2011-12-01 23:15:22
【问题描述】:
我花了一些时间阅读了一些关于审计跟踪的帖子和文章,但仍然无法弄清楚。
我需要的是一个非常基本的审计系统,它会给我如下结果:
- 客户“John Doe”已被“用户”删除
- 客户“Jane Doe”是由“其他用户”创建的
- 来自“John Doe”的地址已被“用户”修改
- 客户“Jane Doe”已被“其他用户”删除
我试图覆盖 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 专家,并且在部署应用程序后我不会拥有对数据库的控制如此之大。
我确定我在这里遗漏了一些非常简单的东西。 感谢您的帮助。
提前致谢。
【问题讨论】:
标签: entity-framework entity-framework-4 ef-code-first tracking ef4-code-only