New Posts New Posts RSS Feed: EntityManager.HasChanges() and  CheckpointManager.RollbackCheckpoints()
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

EntityManager.HasChanges() and CheckpointManager.RollbackCheckpoints()

 Post Reply Post Reply
Author
afrizal.chen View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Dec-2008
Location: Singapore
Posts: 21
Post Options Post Options   Quote afrizal.chen Quote  Post ReplyReply Direct Link To This Post Topic: EntityManager.HasChanges() and CheckpointManager.RollbackCheckpoints()
    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.
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: 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. 
Back to Top
afrizal.chen View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Dec-2008
Location: Singapore
Posts: 21
Post Options Post Options   Quote afrizal.chen Quote  Post ReplyReply Direct Link To This Post 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
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: 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 support@ideablade.com.
 
 
 
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: 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

         
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down