首先使用 EF 4.2 代码拦截并记录更改
我花了一些时间阅读了一些有关审计跟踪的帖子和文章,但仍然无法弄清楚这一点。
我需要的是一个非常基本的审计系统,它会给我结果,例如:
- 客户“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 人员,并且在部署应用程序后我对数据库没有那么多的控制权。
我确信我在这里遗漏了一些非常简单的东西。 我很感激任何帮助。
提前致谢。
I spent some time reading a few posts and articles about audit tracking but still can't figure this out.
What I need is a pretty basic audit system that will give me results such as:
- Customer "John Doe" was deleted by "User"
- Customer "Jane Doe" was created by "Other User"
- Address from "John Doe" was modified by "User"
- Customer "Jane Doe" was removed by "Other User"
I tried to override the SaveChanges event on dbContext but i got stuck with the following problem:
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.
}
I know I could use triggers on the database to accomplish this but I'd like to keep it here so I have more control over it, since I'm not an SQL guy and after deploy the application I won't have that much control over the db.
I'm 'sure I'm missing something very simple here.
I appreciate any help.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以检查上下文中是否存在任何审核条目,并在调用 SaveChanges 时将其删除。它将解决您的第一个问题 - 您始终会在创建新的审核条目之前删除以前的审核条目。
第二个问题无法用 DbContext API 解决。 DbContext API 是 ObjectContext API 的简化,这种简化删除了复杂场景所需的方法。其中一种方法是 SaveChanges 的重载版本,能够不接受更改(不更改实体的状态)。
您可以通过
IObjectContextAdapter
将DbContext
转换为ObjectContext
。即使使用ObjectContext
,它也不会是直接的:You can always check if there are any audit entries in your context and remove them when your SaveChanges is called. It will solve your first issue - you will always remove previous audit entries prior to creating a new one.
The second problem cannot be solved with DbContext API. DbContext API is simplification of ObjectContext API and this simplification removed methods needed for complex scenarios. One such method is overloaded version of
SaveChanges
with ability to not accept changes (not change states of entities).You can convert
DbContext
toObjectContext
throughIObjectContextAdapter
. Even withObjectContext
it will not be straight forward: