Print Page | Close Window

EntityManager.HasChanges() and CheckpointManager.RollbackCheckpoints()

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=1076
Printed Date: 01-May-2025 at 10:11am


Topic: EntityManager.HasChanges() and CheckpointManager.RollbackCheckpoints()
Posted By: afrizal.chen
Subject: EntityManager.HasChanges() and CheckpointManager.RollbackCheckpoints()
Date Posted: 02-Feb-2009 at 3:42am
Current State:
EntityManager.HasChanges() ==> false

This is the scenario:
1. CheckpointManager.BeginCheckPoint()
2. Create a new Entity. Here, EntityManager.HasChanges() ==> true
3. CheckpointManager.RollbackCheckpoints(). Here, CheckpointManager.IsCheckPointing == > false

Actual Result:
EntityManager.HasChanges() ==> true

My Expected Result:
EntityManager.HasChanges() ==> false

My Issue:
I don't know if this behavior is by designed. But I hope there's a way to get my expected result, please advise. :)

Thank you.



Replies:
Posted By: kimj
Date Posted: 04-Feb-2009 at 11:15am
I'm not able to reproduce the problem.  Here's the test I'm using, against the tutorial DB:
 
DomainModelEntityManager em = DomainModelEntityManager.DefaultManager;
Assert.IsFalse(em.HasChanges());
em.CheckpointManager.BeginCheckpoint();
 
Customer aCustomer = em.CreateEntity<Customer>();
em.GenerateId(aCustomer, Customer.IdEntityProperty);
aCustomer.CompanyName = "My new company";
em.AddEntity(aCustomer);
 
Assert.IsTrue(em.HasChanges());
 
em.CheckpointManager.RollbackCheckpoint();
Assert.IsFalse(em.CheckpointManager.IsCheckpointing);
Assert.IsFalse(em.HasChanges());
We haven't specifically fixed any checkpointing issues in current bits, but it's possible you're seeing a problem in your version which has been fixed by other changes.  Which version of DevForce EF are you using?
 
It's also possible that there are some side effects with your specific Entity creation that I don't see with the tutorial entities.  Try this - after determining that the EntityManager is incorrectly reporting changes, query for changed entities, like this:

var list = em.FindEntities(EntityState.Added | EntityState.Deleted | EntityState.Modified);

This may help us diagnose the problem. 


Posted By: afrizal.chen
Date Posted: 22-Feb-2009 at 11:25pm
Hello Kim, Sorry for the late reply.

I have new finding regarding this issue:
1. BeginCheckpoint()
2. Create 3 new Entities
3. var list = em.FindEntities(EntityState.Added); // "list.ToList().Count" returns "3"
4. RollbackCheckpoint()
5. list = em.FindEntities(EntityState.Added); // "list.ToList().Count" returns "2"  now

if I create 2 new Entities in step (2), the 'list.ToList().Count' will return me '1' after rollingback.
It seems that the RollBack is only rolling back 1 entity.
by the way I am using "DevForce EF 4.2.2.3".

Thanks,
Afrizal


Posted By: kimj
Date Posted: 24-Feb-2009 at 12:09pm
Afrizal, I'm still unable to reproduce the problem in the 4.2.2.3 version.  Following your steps with the NorthwindIB tutorial database and a Customer entity, I see that checkpoint/rollback is working as desired and rolls back all added entities.  I really think there may be side effects in your Entity creation logic which are causing the problem. 
 
Please send a sample solution showing the problem to mailto:support@ideablade.com - support@ideablade.com .
 
 
 


Posted By: kimj
Date Posted: 17-Mar-2009 at 2:14pm
Afrizal, this problem is due to the order in which the EntityKey is set and the entity is added to the EntityManager.  Your entity creation methods all do something similar to this:
 
         Person newInstance = manager.CreateEntity<Person>();
         newInstance.EntityAspect.AddToManager();
         newInstance.InternalId = Guid.NewGuid();
         ... set other properties
 
When checkpointing is on the entity key needs to be set prior to adding the entity to cache, otherwise additions and modifications are not correctly tracked, with unpredictable results.  You can easily fix the problem by following this pattern instead:
 
         Person newInstance = manager.CreateEntity<Person>();
         newInstance.InternalId = Guid.NewGuid();
         newInstance.EntityAspect.AddToManager();
         ... set other properties

         



Print Page | Close Window