New Posts New Posts RSS Feed: SaveChanges and EntityManagerSaveException
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

SaveChanges and EntityManagerSaveException

 Post Reply Post Reply
Author
LesleySastro View Drop Down
Newbie
Newbie


Joined: 13-Nov-2008
Posts: 9
Post Options Post Options   Quote LesleySastro Quote  Post ReplyReply Direct Link To This Post Topic: SaveChanges and EntityManagerSaveException
    Posted: 05-Dec-2008 at 12:44pm
 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?
 
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 09-Dec-2008 at 6:52pm
FYI to anyone else having this problem - the error is caused by a DevForce bug which should be fixed in the Jan/Feb 2009 release.  The workaround until then is to ensure that you remove all Unchanged entities from the cache of the EntityManager used by your IEntityServerSaving implementation before exiting the handler.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down