I created a Audit Trail feature by using an implementation of IEntityServerSaving. Basically what I do, is checking for each entity to be saved what action (insert, update or delete) has taken place and what data properties has changed. The action together with the user and a timestamp I store in an AuditDataAction table and the changed properties with their original and new values in an AuditDataFields table (so we have a master-detail relationship). In the IEntityServerSaving class I'm creating and adding the AuditDataAction and AuditDataField entities to the passed in EntityManager for saving. And here is where I get confused. I'm getting the following exception
Object reference not set to an instance of an object.
When debugging and looking at the SaveResult, I'm seeing Cancelled=False, FailureType=Other, and for EntitiesWithErrors I get one entity of type User. This is not the original entity I was saving, and for that matter looking at the EntityState of that User entity I see the state Unchanged, and no errors. This User entity is only used to fill in the user link for the AuditDataAction entity. I am also using the EntityManager.FindEntities(EntityState.Added | EntityState.Modified | EntityState.Deleted) method to find all the entities that needs to be saved, and the User entity is for sure not one of them.
When turning the Audit Trail feature off, I'm not getting any exceptions and the entities are saved correctly into the database. So it should be something that I have done within the IEntityServerSaving class, like adding the audit entities. But I'm not getting anywhere with the error message exposed. Does anyone have any idea how I can better determine the cause of the failure?