0
我的目標是保存和審計日誌同時創建。運行該應用程序變種結果時= base.SaveChnages()後,實體進入狀態改變,如何任何一個可以幫助並欣賞EntityFramework DBContext審計錯誤
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;
}
}