How to get Primary Key from Audit method

Posts   
 
    
Philip
User
Posts: 17
Joined: 01-May-2009
# Posted on: 22-Jul-2011 23:48:46   

Hi there,

I'm using LLBLGen v3.1, SQL Server 2008 R2, and .net 4

I am extending AuditorBase.

I am trying to get the primary key from the newly inserted entity object passed into AuditEntityFieldSet, but the ID comes up 0 for new records.

The code I'm using is simply this:

a.RecordId = ((VolunteerEntity)entity).VolunteerId;

For updated records, I do get the ID key as needed.

Also, I can get the ID for inserts in the AuditInsertOfNewEntity method, so, I assume my understanding that these methods are run AFTER insert and after the entity is refreshed from the database record is true.

The question is then, what am I doing wrong?

Thanks!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Jul-2011 01:42:30   

You are not doing any wrong. It's just that AuditEntityFieldSet is called when an entity field is set, even if it's new and it has not been saved yet.

What you can do is to store the information of the entity field set in some variables of your auditor. Then at AuditInsertOfNewEntity you can get the PK and use the stored information about the entity field set to grab your audits.

This is demonstrated better in the Auditing example. You can download it here.

David Elizondo | LLBLGen Support Team
Philip
User
Posts: 17
Joined: 01-May-2009
# Posted on: 23-Jul-2011 18:12:24   

Perfect! The fact that the field set method gets called as the entity gets set, but before insert into the db makes everything clear. I did as you mentioned and set the ids in the 'insert' method and it works as I need.

Much appreciated!