Clone entity for auditing

Posts   
 
    
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 29-Feb-2012 12:57:21   

I am trying to customize the DatabaseAuditor. One of the things I need to do is to write the complete entity XML to a database field for deletes and updates.

I'm stuck on the method to clone the entity and extract the XML from the clone. I keep getting an ORMEntityOutOfSyncException.

I've tried this code


        private string GetEntityXML(IEntityCore entity)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            formatter.Serialize(stream, entity);
            // seek the stream back to the beginning.
            stream.Seek(0, SeekOrigin.Begin);

            // deserialize the data back into a new instance (and all the related entities)
            IEntityCore clone = (IEntityCore)formatter.Deserialize(stream);
            string xml = string.Empty;
            clone.WriteXml(out xml);
            return xml;
        }

What am I doing wrong?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Feb-2012 14:52:36   

Try setting the entity.Fields.State = EntityState.Fetched;

jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 29-Feb-2012 16:30:20   

That took care of the exception and I am getting the correct entity XML for updates. For the deletes, I only get the primary key. That makes some sense to me since the entity has never been fetched from the database.

How could I do a automatic fetch from the database prior to deleting an entity so that those values would be populated?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Feb-2012 17:18:36   

You should be getting all fields values, not only the PK. Maybe you have an old version of the runtime libraries, so please try the latest one.

The following is quoted from the code sample in the manual.

/// <summary>Audits the successful delete of an entity from the database</summary>
/// <param name="entity">The entity which was deleted.</param>
/// <remarks>As the entity passed in was deleted succesfully, reading values from the 
/// passed in entity is only possible in this routine. After this call, the
/// state of the entity will be reset to Deleted again and reading the fields 
/// will result in an exception. It's also recommended not to reference
/// the passed in entity in any audit entity you might want to persist as the entity 
/// doesn't exist anymore in the database.</remarks>
public override void AuditDeleteOfEntity(IEntityCore entity)
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 29-Feb-2012 17:31:54   

Yes, I saw that comment but that's not what I'm getting.

ORM version is 3.1.11.1105. SQL DQE is 3.1.11.221

I think those are the latest. Am I wrong?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39909
Joined: 17-Aug-2003
# Posted on: 01-Mar-2012 12:41:38   

If the entity is never fetched, the data isn't in the entity and it's not passed to the method. I understand from your posts that the entity to delete isn't fetched from the db? That means only the PK is set (to make the delete work) so the rest of the entity data isn't in the entity object in-memory as it's not read from the DB. If you want that, read the entity first from the DB. The method quoted by Walaa is assuming the entity passed in is fetched from the db first (so the fields are populated)

Frans Bouma | Lead developer LLBLGen Pro