Print Page | Close Window

SaveChanges and EntityManagerSaveException

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1015
Printed Date: 15-Apr-2025 at 9:34am


Topic: SaveChanges and EntityManagerSaveException
Posted By: LesleySastro
Subject: SaveChanges and EntityManagerSaveException
Date 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?
 



Replies:
Posted By: kimj
Date 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.



Print Page | Close Window