none
How do you temporarily suspend change tracking? RRS feed

  • Question

  • I am investigating Linq to entities for an upcomming project upgrade.  The current distributed application receives notification messages via message queues that contain status changes and new RowVersion values. When applying the updates from the messages we need to temporarily suspend database change tracking as these changes are already in the database. I can't seem to find the correct way to do this. Is the AutoDetectChangesEnabled the way to do this?
    Tuesday, September 20, 2011 9:45 PM

Answers

  • In that case, I think what you want to do is simple Detach your object and then reattach at a later time.  Something like this:

    context.MyObjects.Detach(MyObject);
    
    //update some stuff on MyObject
    ....
    
    context.MyObjects.Attach(MyObject);
    
    


    Now, MyObject will have been changed, but the state of the object and properties that got changed will not be marked as "changed" so they won't be persisted back to the database.

     


    Tom Overton
    • Marked as answer by Larcolais Gong Tuesday, September 27, 2011 8:39 AM
    Wednesday, September 21, 2011 2:00 PM

All replies

  • You can do it by setting the MergeOption on your particular entity:

    context.MyEntity.MergeOption = MergeOption.NoTracking;  
    


    Another thing you can do is disable proxy creation, which will also make it so there is no change tracking:

     context.ContextOptions.ProxyCreationEnabled = false;
    



    Tom Overton
    Tuesday, September 20, 2011 11:50 PM
  • Thanks for your answer Tom.

    I don't think it answers the problem as the object I want to suspend from tracking is already in memory and should normally be tracked.

    I need somthing like:

    myObject.SuspendTracking
    some simple assignments from a WCF message that reflects DB changes already made by some other process on some other machine.
    myObject.ResumeTracking

     


    • Edited by Synesis Wednesday, September 21, 2011 1:38 PM
    Wednesday, September 21, 2011 1:36 PM
  • In that case, I think what you want to do is simple Detach your object and then reattach at a later time.  Something like this:

    context.MyObjects.Detach(MyObject);
    
    //update some stuff on MyObject
    ....
    
    context.MyObjects.Attach(MyObject);
    
    


    Now, MyObject will have been changed, but the state of the object and properties that got changed will not be marked as "changed" so they won't be persisted back to the database.

     


    Tom Overton
    • Marked as answer by Larcolais Gong Tuesday, September 27, 2011 8:39 AM
    Wednesday, September 21, 2011 2:00 PM
  • Thanks again.

     

    Wednesday, September 21, 2011 2:13 PM