Answered How to use Attach() to update record?

  • Tuesday, June 24, 2008 5:34 PM
     
     

    Hi, testing the Attach method, I'm trying to update an existing record in my database that has a Topicid of 60.  I'm not sure what I'm doing wrong to have the SubmitChanges work.  There is no error but I'm also not seeing any changes to my record. 

     

    DataClassesDataContext ctx = new DataClassesDataContext();

     

    Topic newTopic = new Topic
    {
         TopicId = 60,
         TopicName = "Change tracking test updated " + System.DateTime.Now.ToString(), 

         TopicTypeId = 1
    };

     

    ctx.Topics.Attach(newTopic);
    ctx.SubmitChanges();

All Replies

  • Wednesday, June 25, 2008 1:05 AM
     
     Answered

    In your case, you must attach your object BEFORE making the changes. Otherwise, the context is oblivious to your changes. The entity does not track it's dirty status. This is done by the context monitoring the INotifyPropertyChanged notifications. By attaching after making the changes, the notification listners are wired-up too late.

     

    As an alterhative, you can use one of the other overloads of Attach which take either the original and current copies of the object, or use an object with a timestamp/rowversion property (Attach(object, true)).

     

    Jim Wooley

    www.ThinqLinq.com

     

  • Wednesday, June 25, 2008 2:19 PM
     
     

    Thanks Jim.

     

    I modified my code and it worked. Dave