New Posts New Posts RSS Feed: Auditing Inserts with Identity columns
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Auditing Inserts with Identity columns

 Post Reply Post Reply Page  <12
Author
Wokket View Drop Down
Newbie
Newbie


Joined: 17-May-2011
Posts: 17
Post Options Post Options   Quote Wokket Quote  Post ReplyReply Direct Link To This Post Topic: Auditing Inserts with Identity columns
    Posted: 26-May-2011 at 5:02pm
G'day Robert,
 
The Audit table is defined as an Entity in our EDMX, but does not have a FKey back to the source table... we have a single audit table for the entire schema, storing the related entity name (GetType().Name) and ID as a non-related int.
 
Our save interceptor currently looks as follows:

///

<summary>

/// This class is hooked into the Save pipeline to allow us to audit changes to data.

/// </summary>

public class AuditingSaveInterceptor : EntityServerSaveInterceptor

{

protected override bool ExecuteSave()

{

var entitiesBeingAdded = EntityManager.FindEntities(EntityState.Added).OfType<Entity>().ToList(); //get a concrete list, not a queryable.

var entitiesBeingUpdated = EntityManager.FindEntities(EntityState.Modified).OfType<Entity>().ToList();

AuditUpdates(entitiesBeingUpdated);

var entitiesBeingDeleted = EntityManager.FindEntities(EntityState.Deleted).OfType<Entity>().ToList();

AuditDeletes(entitiesBeingDeleted);

if (!base.ExecuteSave())

{

return false; //don't go any further if that bit of work failed.

}

//TODO: Audit inserts later on. See http://www.ideablade.com/forum/forum_posts.asp?TID=2717&PID=10825#10825

//AuditInserts(entitiesBeingAdded);

// return base.ExecuteSave(); //doesn't save the new Added audit records, attempts to re-save the items saved above.\

return true;

}

private void AuditDeletes(IEnumerable<Entity> entitiesBeingUpdated)

{

AuditChanges(entitiesBeingUpdated,

"Delete");

}

private void AuditUpdates(IEnumerable<Entity> entitiesBeingUpdated)

{

AuditChanges(entitiesBeingUpdated,

"Edit");

}

private void AuditInserts(IEnumerable<Entity> entitiesBeingAdded)

{

AuditChanges(entitiesBeingAdded,

"Add");

}

private void AuditChanges(IEnumerable<Entity> entitiesBeingUpdated, string auditType)

{

if (Principal == null || Principal.Identity.AuthenticationType == "Anonymous")

{

// we can't audit this without a proper ID to record it against

Debug.Assert(false, "DANGER: Not recording audit information as there's no authentication information available.");

return;

}

var userId = ((HCareIdentity) Principal.Identity).UserID;

foreach (var entity in entitiesBeingUpdated)

{

var audit = EntityManager.CreateEntity<Audit>();

audit.EntityAspect.AddToManager();

audit.AuditTime =

DateTime.Now;

audit.AuditType = auditType;

audit.EntityName = entity.GetType().Name;

audit.EntityID = (

long)entity.GetType().GetProperty("ID").GetValue(entity, null); // All our tables have a "ID" column storing the identity value.

audit.ChangeByID = userId;

}

}

}

 

Back to Top
robertg View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Mar-2011
Location: California
Posts: 87
Post Options Post Options   Quote robertg Quote  Post ReplyReply Direct Link To This Post Posted: 26-May-2011 at 4:56pm
Wokket,
 
Is tue audit table in your entity model, with a FK relationship defined back to your data table? It seems that if it was, you should be able to execute the save in a single transaction, since EF will  create populate the correct information into the navigation properly.
 
If that's not the case, could you post your save interceptor here so we can take a look at it?
 
Thanks,
-- Robert
Back to Top
Wokket View Drop Down
Newbie
Newbie


Joined: 17-May-2011
Posts: 17
Post Options Post Options   Quote Wokket Quote  Post ReplyReply Direct Link To This Post Posted: 25-May-2011 at 5:41pm
G'day again guys,
 
I'm chasing some advice about auditing inserts of data where we are using Identity columns in the database for ID's.
 
I've created an EntityServerSaveInterceptor derived class, overrriden ExecuteSave() and am successfully auditing updates to existing data.
 
When it comes to inserts however, we (obviously) cannot get the ID value of the freshly inserted row until after the call to base.ExecuteSave() as we're using Identity columns.
 
I've attempted to call base.ExecuteSave() a second time to insert the Audit records, however they don't seem to be detected as pending entities, the system attempts to insert the original data again.
 
Variants of EntityManager.SaveChanges() fail due to recursive saves not being supported.
 
Can anyone shed some light on whether this is possible? It seems like a pretty common requirement, I'm coming up short atm.
 
Cheers,
 
 
Back to Top
 Post Reply Post Reply Page  <12

Forum Jump Forum Permissions View Drop Down