This project is read-only.

Association Tables Primary Keys

Nov 4, 2009 at 3:00 PM
Edited Nov 4, 2009 at 3:04 PM

So far this looks like it will work for me.  Thanks for making this available!

In my application I want to log when a task is added to a group (I have tables group and grouptasks).  I added this definition:  

this.Groups.Audit().AuditAssociation<GroupTask>(g => g.GroupTasks);
 
The resulting record in AuditRecords:
 
ID Action EntityTable EntityTableKey AssociationTable AssociationTableKey AuditDate UserName
45 0 Group 5 GroupTask 0 11/3/2009 10:29:00 AM test
 
AssociationTableKey was 0, but I want it to be the PK of the GroupTask that was added.  
 
So, I modified AuditableDatacontext.SubmitChanges():
      if (record.Action == AuditAction.Insert)
           {
		int pk = (int)record.KeySelector.Compile().DynamicInvoke(record.Entity);
                  	if (record.EntityTableKey == 0)
                      record.EntityTableKey = pk;

                  	if (record.AssociationTableKey == 0)
                      record.AssociationTableKey = pk;
          }
 
That seemed to accomplish my goal.  (Should that be an else if?)
 
My next step is to add the primary key to AuditRecordFields.  This is what I'm getting now:
 
Id AuditRecordId MemberName OldValue NewValue
157 45 Group TaskID   0
158 45 GroupID   5
159 45 TaskID   16
160 45 Group   TrainingTracker6.Group
161 45 Task   Emergency Communications
 
What I want is for GroupTaskID to show the primary key for that database record, rather than 0.
 
One solution -- just under the code above, loop through record.ModifiedProperties, find the one that matches the primary key, and update.  My problem here is, I don't know how to get the name of the primary key programmatically.  I can see it when I'm debugging -- record.KeySelector is a lambda expression {e => e.GroupTaskID}.  But, I don't how to get GroupTaskID in a way that I can use it.
 
Any thoughts on how to code this?  Or is there a different approach to take?
 
 
Nov 4, 2009 at 4:02 PM

Hello,

I'm glad you were able to modify the code to suit your needs. That should have been in the code base originally, but should be cleaned up when I get around to releasing the next version.

To solve your issue right now, there is a method you should be able to take advantage of.

IAuditableDataContext has a method:

PropertyInfo GetEntityPrimaryKey<TEntity>();


So you should be able to do:

PropertyInfo pkInfo = this.GetEntityPrimaryKey<GroupTask>();

Please let me know if that isn't what you're looking for and I will try and come up with another solution.

-Matt

Nov 7, 2009 at 4:49 PM

Thanks for the suggestion.  I think will do what I want. 

My issue is I don't have access to the "GroupTask" type when compiling DoddleAudit.  I'm trying to figure out a way to grab that information at runtime? That way it will work with a few other master-detail records in the system. Generics and reflection are still a bit fuzzy for me, but I'm getting there.

Thanks.