[英]access id of added entity before SaveChanges() is called
i'm using EF 4.3.1, I've overridden the SaveChanges() on the context so that I can get a list of the objects and their states and create entries in my audit log table.我正在使用 EF 4.3.1,我已经覆盖了上下文中的 SaveChanges(),以便我可以获得对象及其状态的列表并在我的审计日志表中创建条目。 I need to store the id of the record in the audit log table so i have a reference to it.
我需要将记录的 id 存储在审计日志表中,以便对其进行引用。 This is a problem when records are inserted, as I don't have access to the id before it's saved.
这是插入记录时出现的问题,因为在保存之前我无法访问 id。 Is there any way of getting the id at that point?
有什么方法可以在那时获得 id 吗?
public override int SaveChanges()
{
ChangeTracker.DetectChanges();
var objectStateManager = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager;
var modifiedAuditableEntities = objectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added).Where(e => (IAuditable)e.Entity != null);
foreach (var entry in modifiedAuditableEntities)
{
var entity = (IAuditable)entry.Entity;
if (entity != null)
{
switch (entry.State)
{
case EntityState.Added:
entity.IsAdded = true;
break;
case EntityState.Deleted:
entity.IsDeleted = true;
break;
case EntityState.Modified:
entity.IsModified = true;
break;
}
this.EntitySet<AuditLogEntry>().Add(this.auditLogService.CreateAuditLogEntryForEntity((IAuditable)entry.Entity));
}
return base.SaveChanges();
}
If your goal is that you want both your save, and your audit log to be created at the same time, you could wrap it in a transaction scope so your method to be atomic.如果您的目标是希望同时创建保存和审核日志,则可以将其包装在事务范围中,以便您的方法是原子的。
public override int SaveChanges()
{
ChangeTracker.DetectChanges();
using (var scope = new TransactionScope())
{
var objectStateManager = ((IObjectContextAdapter) this).ObjectContext.ObjectStateManager;
var modifiedAuditableEntities =
objectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added).Where(
e => (IAuditable) e.Entity != null);
var result = base.SaveChanges();
foreach (var entry in modifiedAuditableEntities)
{
var entity = (IAuditable) entry.Entity;
if (entity != null)
{
switch (entry.State)
{
case EntityState.Added:
entity.IsAdded = true;
break;
case EntityState.Deleted:
entity.IsDeleted = true;
break;
case EntityState.Modified:
entity.IsModified = true;
break;
}
this.EntitySet<AuditLogEntry>().Add(
this.auditLogService.CreateAuditLogEntryForEntity((IAuditable) entry.Entity));
}
}
base.SaveChanges();
scope.Complete();
return result;
}
}
Id the Id is an integer generated by the database there isnt any way of getting it before calling save changes. Id Id 是数据库生成的整数,在调用保存更改之前没有任何方法可以获取它。 Possible solutions include:
可能的解决方案包括:
I would recommend using a stored procedure at the database level to perform your insert/auditing functions (and other database changes), and revoking the insert/update/delete permission from your users.我建议在数据库级别使用存储过程来执行插入/审核功能(和其他数据库更改),并撤销用户的插入/更新/删除权限。 As such you can guarantee the integrity of the update process and of the audit table.
因此,您可以保证更新过程和审计表的完整性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.